How to parse xml using jQuery?

What is the jQuery alternative for the following javascript code?

var xmlobject = (new DOMParser()).parseFromString(xmlstring, "text/xml"); 

I believe jQuery alternative will be more compatible with multiple browsers?

+4
source share
3 answers

The cross-browser approach is as follows, which I posted a few minutes ago in response 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); } 
+3
source

Take a look at these plugins:

xmlDOM - http://plugins.jquery.com/project/XmlDOM
jParse - http://jparse.kylerush.net/

0
source

var $parsedXml = $(xmlstring);

For exmaple, if you have something like

 <object> <property id="prop1" value="myVal" /> </object> 

like your xmlstring you could do

var prop1 = $(xmlstring).find('#prop1').attr('value');

to get the value of the property of the object.

-1
source

Source: https://habr.com/ru/post/1312982/


All Articles