Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: Text data outside of root node. #118

Closed
sedenardi opened this issue Jan 6, 2014 · 11 comments
Closed

Error: Text data outside of root node. #118

sedenardi opened this issue Jan 6, 2014 · 11 comments
Assignees

Comments

@sedenardi
Copy link

When parsing I occasionally receive this:

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: Text data outside of root node.
Line: 24
Column: 1
Char:

I am using the module in the "Shoot-and-forget" method:

parseString(rawOuter, function finishXMLParse(err, pRes) {
  if (err) {
    //log error
  } else {
    //do work
  }
});

Am I missing something or is this a bug?

@Leonidas-from-XIV
Copy link
Owner

This looks like your XML data is invalid. Could it be that it looks like this?

<root>foo</root>
bar

@sedenardi
Copy link
Author

Oh I'm sure that the XML is screwed up, but I'm wondering why it's throwing an error event instead of using the returned "err" object? If this is intended behavior, how do I handle this parsing error so bad XML doesn't crash my program?

@sedenardi
Copy link
Author

Hey man, could you take a look at my previous question and advise? I'd just like to be able to handle this XML parsing error.

@toddrbryan
Copy link
Contributor

Same issue here. We have to parse XML coming from outside sources and some of that external XML is bad.

Looks like the exception originates in sax:

Thu Jan 09 2014 14:34:56 GMT-0800 (PST) global exception handler caught: Error: Text data outside of root node.
Line: 0
Column: 36
Char: "
  at error (/Users/bryan/SeaSketch/node_modules/xml2js/node_modules/sax/lib/sax.js:642:8)
  at strictFail (/Users/bryan/SeaSketch/node_modules/xml2js/node_modules/sax/lib/sax.js:662:22)
  at Object.write (/Users/bryan/SeaSketch/node_modules/xml2js/node_modules/sax/lib/sax.js:960:13)
  at Parser.exports.Parser.Parser.parseString (/Users/bryan/SeaSketch/node_modules/xml2js/lib/xml2js.js:386:29)
  at Parser.__bind [as parseString] (/Users/bryan/SeaSketch/node_modules/xml2js/lib/xml2js.js:6:61)
  at exports.parseString (/Users/bryan/SeaSketch/node_modules/xml2js/lib/xml2js.js:409:19)
  at ProbeManagerInternal._wms_http_request (/Users/bryan/SeaSketch/helpers/probemanager.coffee:609:20)
  at Domain.bind.b (domain.js:201:18)
  at Domain.run (domain.js:141:23)
  at ProbeManagerInternal._wms_http_request (/Users/bryan/SeaSketch/helpers/probemanager.coffee:607:20)

@Leonidas-from-XIV
Copy link
Owner

Well you could wrap it in a try-catch. But I agree, this looks quite like a bug in xml2js and should be handled by the err parameter. Will look into it, thanks for the report.

@toddrbryan
Copy link
Contributor

Found it. It's a subtle thing in the interaction with Sax.

Here's a Gist of what I added: https://gist.github.com/toddrbryan/8460378

I can do a pull request if you wish.

Here's the sequence of events as I understand it: Sax begins parsing bad XML which has multiple errors. It throws its custom error event (not a Node EventEmitter event) and xml2js catches it, decides if it has already thrown a single error for this sax parser, and then throws its own error event accordingly. But the Sax error isn't cleared by calling saxParser.resume() so that the next time Sax enters write(), it throws a JS error (as it does when a previous error hasn't been cleared by resume() ).

Additionally, xml2js does not throw an end event when the saxParser emits an onend, causing parseString() to hang forever. To fix that I added a handler for Sax's end event in xml2js.

@Leonidas-from-XIV
Copy link
Owner

@toddrbryan 👍 If you add a regression test, I'd be more than happy to merge your pull request!

@deiga
Copy link

deiga commented Mar 10, 2014

@toddrbryan @Leonidas-from-XIV How's this coming along?

@toddrbryan
Copy link
Contributor

Hey there -

I'm sorry, I let this slip my mind. I'll get a test going and get you a
pull request. Sorry it's taken so long.

Best -

Todd

On Mon, Mar 10, 2014 at 1:36 PM, Timo Sand notifications@github.com wrote:

@toddrbryan https://github.com/toddrbryan @Leonidas-from-XIVhttps://github.com/Leonidas-from-XIVHow's this coming along?

Reply to this email directly or view it on GitHubhttps://github.com//issues/118#issuecomment-37230909
.

@elliotstokes
Copy link

Great stuff @toddrbryan. Looking forward to getting my grubby hands on this at some point.

Shepless added a commit to Shepless/node-tvdb that referenced this issue Mar 21, 2014
Needed to modify the package.json to ensure the Xml2Js package was only
updating to the latest v0.2.x as there is a defect in 0.4.x where
malformed XML throws an exception. See this issue:
Leonidas-from-XIV/node-xml2js#118
Shepless added a commit to Shepless/node-tvdb that referenced this issue Mar 21, 2014
Needed to modify the package.json to ensure the Xml2Js package was only
updating to the latest v0.2.x as there is a defect in 0.4.x where
malformed XML throws an exception. See this issue:
Leonidas-from-XIV/node-xml2js#118

There is also an issue using coffee script 1.7.x, the coffee script
only compiles going unto 1.6.x so I have made that modification also.
@Leonidas-from-XIV
Copy link
Owner

This was merged couple days ago and is part of 0.4.2, so closing that issue now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants