Leonidas-from-XIV / node-xml2js

XML to JavaScript object converter.
MIT License
4.84k stars 596 forks source link

Error occurs when using empty nodes: Text data outside of root node #699

Open cheenamalhotra opened 7 months ago

cheenamalhotra commented 7 months ago

Here's a repro:

var xml2js = require('xml2js');
var data = '<p>hello world</p><p> </p><p>&nbsp;</p>';
var parser = new xml2js.Parser();
parser.parseString(data, function (err, result) {
    console.dir(result);
});
Error: Text data outside of root node.
Line: 0
Column: 33
Char: &

Alternatives that work

It would be good if expected behaviors are documented.

Leonidas-from-XIV commented 7 months ago

I am not sure what to document, because the example that you posted is not well-formed XML to begin with so not sure what the library should do in case of invalid input data.

cheenamalhotra commented 7 months ago

If by well-formed XML, you mean a parent root node is required, that should be something to begin with. And secondly, if multiple root nodes are not allowed, it should consistently fail.

Leonidas-from-XIV commented 7 months ago

Yes, to be valid XML you need one single root node, otherwise it's not XML.

But yeah, it should consistently fail on multiple root nodes, that's for sure. If it doesn't then it is a bug.