Dealing with incomplete XML files
I am using jQuery to poll an XML file for changes.
Every now and again the XML file is incomplete (the server has only partially written it out). Unfortunately jQuery seems to carry on as if it's the XML is ok meaning my code errors when it tries to find attributes / tags that do not exist.
How can I tell whether an XML file is complete or not when returned f开发者_如何学编程rom jQuery?
There's disscusion on the jQuery forums here regarding checking for well formed XML. The comments appear to suggest that there is no reliable method available at the moment.
Could you use a try/catch block or write some code to sense check the XML before attempting to parse it?
I am not sure if what you are looking is how to check a valid XML using jQuery.
If it is, I have created a simple function that utilizes jquery parseXML
:
function validXML(xml) {
try {
var xmlDoc = $.parseXML(xml);
return true;
} catch (e) {
return false;
}
}
var correctXML = "<rss version='2.0'><channel><title>RSS Title</title></channel></rss>";
var incorrectXML = "<rss version='2.0'><channel><title>RSS Title</title></rss>"; // missing </channel>
console.log(validXML(correctXML));
console.log(validXML(incorrectXML));
精彩评论