Leonidas-from-XIV / node-xml2js

XML to JavaScript object converter.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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>&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

  • <div><p>hello world</p><p> </p><p>&nbsp;</p></div> (wrap into root node)
  • <p>hello world</p><p>&nbsp;</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.

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.