Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I test for XML using jQuery

Tags:

jquery

xml

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 following:

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

But not sure.

like image 209
HGPB Avatar asked Oct 08 '11 18:10

HGPB


People also ask

Does jQuery work with XML?

No, JQuery HTML doesn't work with XML document. It only works for HTML documents.

What is jQuery XML?

jQuery. parseXML uses the native parsing function of the browser to create a valid XML Document. This document can then be passed to jQuery to create a typical jQuery object that can be traversed and manipulated.


1 Answers

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
}
like image 83
andyb Avatar answered Sep 22 '22 14:09

andyb