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. #255

Closed
cubesapp opened this issue Nov 4, 2015 · 11 comments
Closed

Error: Text data outside of root node. #255

cubesapp opened this issue Nov 4, 2015 · 11 comments

Comments

@cubesapp
Copy link

cubesapp commented Nov 4, 2015

I am getting an exception thrown with the latest release v0.4.15. Obviously the xml being passed is badly formed. To my understanding this was fixed in #118

var parser = new xml2js.Parser();

parser.parseString(data,function(err, result) {
// do something;
});

@tflanagan
Copy link
Contributor

Can you provide a sample snippet that repos this issue?

@compkidchris
Copy link

Also having this issue. I have some malformed XML coming in via a stream, and this exception is thrown every so often. Is there anyway to handle the error rather than allowing it to crash my app? The relevant exception returns the following message:

When exiting:
Error: Text data outside of root node.
Line: 0
Column: 40
Char: 1

err passed in the callback
Error: Parse Error

Was also under the impression this was previously solved.

@tflanagan
Copy link
Contributor

@compkidchris and @cubesapp, we need sample code to replicate the issue

@compkidchris
Copy link

A more verbose error would be:

Offending XML:
<trade><cvol>9950902</cvol><datetime>2015-11-16T12:54:02-05:00</datetime><exch>NASDAQ Intermarket</exch><last>6.1
Purported Error:
[Error: Unclosed root tag
Line: 0
Column: 113
Char: ]
Offending XML:
</last><symbol>CHK</symbol><tcond>57,16</tcond><timestamp>1447696442</timestamp><vl>100</vl><vwap>6.083479</vwap></trade>
Purported Error:
[Error: Unmatched closing tag: last
Line: 0
Column: 7
Char: >]
Final Error:
Error: Text data outside of root node.
Line: 0
Column: 35
Char: 5
Error: Parse Error

The XML here is split across multiple calls to the parser, each snippet following "Offending XML: " is what is passed to the parser, Purported Error is the err from the parseString callback. The Final Error is the error/exception that ends my app. Let me know if this is helpful, I can provide some simple code which exemplifies this issue if need be.

Edit: Don't know how to format for code, there are tags where there are Offending XML.
Edit2: Figured it out.

@tflanagan
Copy link
Contributor

@compkidchris, am I understanding this correctly?

You are streaming an XML document and are trying to parse it with each chunk you receive?

If that assumption is correct then change your workflow to parse after all chunks are gathered. parseString can't handle streams, due to underlying dep limitations.

@compkidchris
Copy link

Will do, not sure if @cubesapp has another issue entirely though.

@cubesapp
Copy link
Author

I haven't been able to reproduce the issue lately. So I don't have the xml and code snippet yet! We have made code changes on our end recently to ensure that MOST XML documents being passed to parseString are valid.

Sending partial XML documents via stream to parseString seems to be the issue (which we were doing). Ideally, if parseString returns an error instead of throwing an exception would be best; in order to handle this issue.

@tflanagan
Copy link
Contributor

@cubesapp I just want to reiterate that parseString cannot handle streams. You must pass in the entire XML document for a successful parse.

Ideally, if parseString returns an error instead of throwing an exception would be best; in order to handle this issue.

I don't understand what you mean by returns an error. parseString is synchronous, so to catch any errors, as with any other nodejs sync function, you must wrap it in a try/catch

@cubesapp
Copy link
Author

I always assumed that the call was async.

So in the following:

var parser = new xml2js.Parser();
parser.parseString(data,function(err, result) {
// do something;
});

Would I wrap it in an a try/catch? If this is the case, thanks and I am good to go with this.

@jcsahnwaldt
Copy link
Contributor

@cubesapp

Would I wrap it in an a try/catch? If this is the case, thanks and I am good to go with this.

That's correct. See #67 and #408 for details.

@jcsahnwaldt
Copy link
Contributor

I guess this can be closed...

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