开发者

Catch Malformed XML in Javascript

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜