Hikaru Shindo
Hikaru Shindo

Reputation: 2923

xml2js: can't set option in parseString() function

<doc>
  <people>
  <name sex="male">Harry Potter</name>
  <age>18</age>
  </people>
  <people>
  <name sex="male">hermione granger</name>
  <age>18</age>
  </people>
</doc>

From example xml I use xml2js to parse to object and I want to change the attribute name then I set the option in parseString() function but when I add any options in the function it doesn't work anymoe.

var xml2js = require('xml2js').Parser();

xml2js.parseString(xml,{attrkey:'att'}, function(err, result){
    if(err) {throw err;}
    console.log(JSON.stringify(result));                        
});

When I add option {attrkey:'att'} or others option to the function, it won't work. Do I have something wrong ?

Thanks for help !

Upvotes: 1

Views: 2873

Answers (1)

Zali
Zali

Reputation: 311

i've faced the same issue, so if it is still relevant, it should be like this:

var xml2js = require('xml2js').Parser({attrkey:'att'});
xml2js.parseString(xml, function(err, result){
    if(err) {throw err;}
    console.log(JSON.stringify(result));                        
});

there is an example here

Upvotes: 5

Related Questions