What is the jQuery alternative to the following JavaScript code?
var xmlobject = (new DOMParser()).parseFromString(xmlstring, "text/xml");
I believe a jQuery alternative would be more cross-browser compatible?
What is the jQuery alternative to the following JavaScript code?
var xmlobject = (new DOMParser()).parseFromString(xmlstring, "text/xml");
I believe a jQuery alternative would be more cross-browser compatible?
The cross-browser approach is the following, which I posted a few minutes ago in answer to a similar question:
var parseXml;
if (window.DOMParser) {
parseXml = function(xmlStr) {
return ( new window.DOMParser() ).parseFromString(xmlStr, "text/xml");
};
} else if (typeof window.ActiveXObject != "undefined" && new window.ActiveXObject("Microsoft.XMLDOM")) {
parseXml = function(xmlStr) {
var xmlDoc = new window.ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = "false";
xmlDoc.loadXML(xmlStr);
return xmlDoc;
};
} else {
parseXml = function() { return null; }
}
var xml = parseXml("<foo>Stuff</foo>");
if (xml) {
window.alert(xml.documentElement.nodeName);
}
var $parsedXml = $(xmlstring);
For exmaple, if you have something like
<object>
<property id="prop1" value="myVal" />
</object>
as your xmlstring
, you could do
var prop1 = $(xmlstring).find('#prop1').attr('value');
to get the value of the object property.
Take a look at these plugins:
xmlDOM - http://plugins.jquery.com/project/XmlDOM
jParse - http://jparse.kylerush.net/