开发者

How can I test for XML using jQuery

开发者 https://www.devze.com 2023-04-11 12:02 出处:网络
How can I test for XML using jQuery? I have a bookmarklet that needs to know whether the current page is XML. How can I check for this?

How can I test for XML using jQuery?

I have a bookmarklet that needs to know whether the current page is XML. How can I check for this?

I imagine I will pass in the document and check for the f开发者_如何学Collowing:

<?xml version="1.0" encoding="UTF-8"?>

But not sure.


Not sure if you want to load the page again, but you could run it through jQuery.parseXML(), which will throw an exception for invalid XML, so you could just try to parse the page and if no exception is thrown then it is (valid) XML.

For example:

var xml = "<rss version='2.0'><brokenxml></rss>";
try {
    xmlDoc = $.parseXML(xml);
} catch (err) {
    // was not XML
}
0

精彩评论

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