Leonidas-from-XIV / node-xml2js

XML to JavaScript object converter.
MIT License
4.87k stars 601 forks source link

how can we handle xsi:nil ? #568

Closed sibelius closed 6 months ago

sibelius commented 4 years ago

I have an xml with this value on it

<erroExecucao
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:nil="true"
        />

the returned json is like this:

erroExecucao: [
                      {
                        '$': {
                          'xmlns:xsi': 'http://www.w3.org/2001/XMLSchema-instance',
                          'xsi:nil': 'true'
                        }
                      }
                    ]

how can I transform this into null?

thalysonalexr commented 2 years ago

any solution for this?

rsolomon commented 6 months ago

Set the ignoreAttrs parser option to true. Something like:

import { parseStringPromise } from 'xml2js';

...
const rawXml = /* ... your raw XML */
const parsed = await parseStringPromise(rawXml, {
  ignoreAttrs: true,
  explicitArray: false,
});

Available parser options are documented here: https://www.npmjs.com/package/xml2js#options