Error occurs when using empty nodes: Text data outside of root node
cheenamalhotra opened this issue · comments
Here's a repro:
var xml2js = require('xml2js');
var data = '<p>hello world</p><p> </p><p> </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
<div><p>hello world</p><p> </p><p> </p></div>
(wrap into root node)<p>hello world</p><p> </p><p>something</p>
(not use empty string)- This reads only first node and doesn't fail with error, so I'm not sure if this is also supposed to fail, due to absence of root node?
It would be good if expected behaviors are documented.
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.
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.
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.