Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom Objects for Tags #164

Open
swaredev opened this issue Dec 10, 2014 · 5 comments
Open

Custom Objects for Tags #164

swaredev opened this issue Dec 10, 2014 · 5 comments

Comments

@swaredev
Copy link

Hello -

Is it possible to have the parser utilize my prebuilt objects when parsing. I've tried using the tagNameProcessors config and it 'sort of' works. I get an object that I define but it's always listed as [object Object] = object when I debug into the return value from the parser

I'm new to Javascript so I'm not sure if what I want is possible and if it is how one would go about it.

Tx for any help.

greg

@Leonidas-from-XIV
Copy link
Owner

Have you tried outputting your opject with console.log(util.inspect(r, false, 10))? That should display your object recursively, at least for 10 levels.

Well, your case is not really supported and what you are doing might work, but it might not just as well. I'd probably just let xml2js do its thing and then merge the resulting object with whatever object you already have.

@swaredev
Copy link
Author

Tx for responding.

As a solution that seems to work what I do is

Use xml2js to convert my XML
JSON.stringify to turn into string
JSON.parse with a reviver function that maps the tags to objects and then use the extends project to copy the properties into the new object.

It would be nice to be able to bypass the reconversion into string and the re-parse. So it seems like it would be cool if xml2js had the 'reviver' option ;-)

Am I making this more complicated than needed ?

Tx

@Leonidas-from-XIV
Copy link
Owner

I have no idea why you would convert to a string. Maybe you should describe what this "reviver" is, because you might be right and doing it more complicated than required.

@swaredev
Copy link
Author

Let’s say I start with this XML

John Doe Anna Smith Peter Jones

I have a class in Javascript called Employee that has all kinds of methods built for it.

When I use XML2JS to convert to JSON the ‘employee’ tag is converted to a generic object.

I tried using the tagNameProcessors and a function similar to
function convert(name)
{
if(name === “employee”)
return new Employee();

return name
}

That did seem to work.

However if I use the JSON.parse method with a reviver I seem to get exactly what I think I need.

function ConvertTags(key, value)
{
if(key === “employee") {

    return extend(true,new Employee(),value);

}

return value;

}

As far as I can tell XML2JS.parse returns an object and JSON.parse needs a string so hence the conversion to string first.

@swaredev
Copy link
Author

Sorry XML example didnt post

<employees>
    <employee>
        <firstName>John</firstName> <lastName>Doe</lastName>
    </employee>
    <employee>
        <firstName>Anna</firstName> <lastName>Smith</lastName>
    </employee>
    <employee>
        <firstName>Peter</firstName> <lastName>Jones</lastName>
    </employee>
</employees>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants