开发者

Catch Malformed XML in Javascript

开发者 https://www.devze.com 2022-12-20 19:06 出处:网络
I\'m using prototype to make Ajax requests. Occasionally the responses contain malformed XML. Prototype is usi开发者_开发技巧ng the onException callback, but the first error that is thrown is when I a

I'm using prototype to make Ajax requests. Occasionally the responses contain malformed XML. Prototype is usi开发者_开发技巧ng the onException callback, but the first error that is thrown is when I am trying to access a particular node.

I know Firefox recognizes the malformed response, because if I request the xml via the address bar, Firefox presents the error.

Is there a way for me to catch a "malformed xml" error in javascript?


With javascript typically you're relying on the browser to parse the XML for you. If the browser can't parse it due to it being malformed, you're going to have to tackle it manually. There appears to be a library for doing that at http://xmljs.sourceforge.net/ . I haven't used it myself, but it appears solid. Then again, it might also throw malformed xml errors at you.

What's causing the malformed xml? Maybe there's something you can do on that end?

And finally, if you're just trying to access some part of the document's data, you could consider using a regular expression:

doc = "<one><two>three</two></one>";
captures = doc.match(/<two>(.*)<\/two>/); // returns ["<two>three</two>", "three"]
data = captures[1];                       // "three"
0

精彩评论

暂无评论...
验证码 换一张
取 消