3

xml below to be parsed:

<ENVELOPE>
<ABC>
    <DEF>apple</DEF>
    <GHI>4</GHI>
    <JKL>85</JKL>
</ABC>
<MNO>add</MNO>
<PQR></PQR>
<STU></STU>
</ENVELOPE>

and response after parsing:

{

"ENVELOPE": { "ABC": { "DEF": "apple", "GHI": "4", "JKL": "85" }, "MNO": "add" } }

The other two tags has been discarded by the xml2js parses. But i need it there to be empty atleast. Have tried using options of the parser but no luck yet

Here is my code to get the parser:

private getParser() {
let nameToLowerCase = (name: string): string => {
  return name.toLowerCase();
}

let options: xml2js.Options = {
  attrkey: '_',
  charkey: '$',
  explicitCharkey: true,
  emptyTag: null,
  normalize: true,
  normalizeTags: true,
  explicitArray: false,
  trim: true,
  tagNameProcessors: [nameToLowerCase],
  attrNameProcessors: [nameToLowerCase]
};

return new xml2js.Parser(options);

}

Ankur
  • 677
  • 1
  • 7
  • 21
  • I get pretty different output using [`xml2js`](https://github.com/Leonidas-from-XIV/node-xml2js) using its default settings, so either you're passing it particular options that are causing this, or you're using another package. Can you show the gist of your code? – robertklep Apr 27 '17 at 16:53
  • 1
    Using that exact code (minus the TS annotations), I get `{"envelope":{"abc":{"def":{"$":"apple"},"ghi":{"$":"4"},"jkl":{"$":"85"}},"mno":{"$":"add"},"pqr":null,"stu":null}}` as a result. – robertklep Apr 28 '17 at 10:50
  • It has to be this output. But i m not getting the same. the value with null is not shown – Ankur Apr 28 '17 at 12:07
  • 1
    Which version of `xml2js` are you using (`npm ls xml2js`)? I'm using 0.4.17. – robertklep Apr 28 '17 at 12:08
  • @robertKlep yes the same version. god damn it was my removeUnwantedTags doing the ugly job. No issue in the parser as such. Thanks for the support – Ankur Apr 29 '17 at 06:07

0 Answers0