Home  >  Article  >  Web Front-end  >  js parsing xml string and xml document implementation principle and code (for ie and firefox)_javascript skills

js parsing xml string and xml document implementation principle and code (for ie and firefox)_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:42:24943browse

We have parsed XML documents and XML strings for IE and Firefox respectively. All codes are commented out. If you want to see some functions, just remove the comments.

As for parsing xml in an ajax environment, the principle is actually the same, except that if it is placed in ajax, the returned xml still needs to be parsed.

Copy code The code is as follows:




New Document



Use js to parse xml documents and xml strings


<script> <br>//Parse xml document//////////////////////////////////////////////////// ///// <br>var xmlDoc=null; <br>//Support IE browser <br>if(window.ActiveXObject){ <br>xmlDoc=new ActiveXObject("Microsoft.XMLDOM"); <br> } <br>//Support Mozilla browser<br>else if(document.implementation && document.implementation.createDocument){ <br>xmlDoc = document.implementation.createDocument('','',null); <br> } <br>else{ <br>alert("here"); <br>} <br>if(xmlDoc!=null){ <br>xmlDoc.async = false; <br>xmlDoc.load("house. xml"); <br>} <br>//IE and Firefox not only have different parsers, but also different parsing processes. As follows; <br>//ie parse xml document<br>//alert(xmlDoc.getElementsByTagName("address")[0].childNodes[0].childNodes[0].childNodes[0].nodeValue);// Pop up 1.5 million<br>//alert(xmlDoc.getElementsByTagName("address")[0].childNodes[0].childNodes[1].childNodes[0].nodeValue);//pop up one bedroom and three bedrooms<br> //Traverse and parse childNodes[1] <br>//alert(xmlDoc.childNodes[1].childNodes[1].childNodes[0].childNodes[0].nodeValue);//pop up 2 million <br> //alert(xmlDoc.childNodes[1].childNodes[0].childNodes[0].childNodes[0].nodeValue); //1.5 million pops up<br>//alert(xmlDoc.childNodes[1].childNodes[ 0].childNodes[1].childNodes[0].nodeValue);//Pop up one room and three bedrooms<br>//You can also use item(i) to traverse<br>//var nodes=xmlDoc.documentElement.childNodes ; <br>//alert(nodes.item(0).childNodes.item(0).childNodes.item(0).text); //1.5 million pops up<br>//alert(nodes.item(0) .childNodes.item(1).childNodes.item(0).text); //Pop up one room and three bedrooms<br>//Firefox parses xml document<br>//Firefox and IE parse xml for different nodes Use textContent for the value. <br>//And it will add "n" line breaks before and after some hierarchical child nodes. (I don’t know why. It looks like this when debugging with firebug, so it is best to test the code you have written. It will not be correct in another environment) <br>//In other words, the first node is "n", and the first node is "n". 2 nodes are the real first node. <br>//The third node is "n", and the fourth node is the real second node. <br>//Get and parse childNodes[0] layer by layer <br>//alert(xmlDoc.childNodes[0].childNodes[1].childNodes[1].textContent);//pop up 1.5 million<br>// alert(xmlDoc.childNodes[0].childNodes[1].childNodes[3].textContent);//Pop up a three-bedroom apartment<br>//Get the node name resolution directly getElementsByTagName("address") <br>// alert(xmlDoc.getElementsByTagName("address")[0].textContent);//Pop up 1.5 million for one bedroom and three bedrooms for 2 million and 3 million <br>//alert(xmlDoc.getElementsByTagName("address")[0].childNodes [1].textContent);//Pop up 1.5 million for one room and three bedrooms<br>//alert(xmlDoc.getElementsByTagName("address")[0].childNodes[1].childNodes[1].textContent);// Pop up 1.5 million<br>//alert(xmlDoc.getElementsByTagName("address")[0].childNodes[1].childNodes[3].textContent);//Pop up one room and three bedrooms<br>//alert(xmlDoc .getElementsByTagName("address")[0].childNodes[3].textContent);//pop up 2 million <br>//Firefox can also use the item(1) function to traverse. Note that some hierarchical nodes are also added before and after Node "n". <br>//The first node is item(1), the second node is item(3), and the third node is item(5) <br>//item(1) function traversal analysis<br>/ /var nodes=xmlDoc.documentElement.childNodes; <br>//alert(nodes.item(1).textContent); //pop up 1.5 million one-bedroom, three-bedroom <br>//alert(nodes.item(1). childNodes.item(1).textContent); //1.5 million pops up<br>//alert(nodes.item(1).childNodes.item(3).textContent); //One bedroom and three bedrooms<br>// Parse xml string ////////////////////////////////////////////////// //////////////////////////// <br>var str="<car>" <br>"<brand><price&gt ;500,000</price><pattern>A6</pattern></brand>" <br>"<brand><price>650,000</price><pattern>A8</pattern&gt ;</brand>" <br>"<brand><price>170,000</price></brand>" <br>"</car>"; <br>//Cross-browse The parsers used by IE and Firefox to parse xml are different.<br>var xmlStrDoc=null; <br>if (window.DOMParser){// Mozilla Explorer <br>parser=new DOMParser(); <br>xmlStrDoc=parser.parseFromString(str,"text/xml"); <br>}else{// Internet Explorer <br>xmlStrDoc=new ActiveXObject("Microsoft.XMLDOM"); <br>xmlStrDoc.async="false"; <br>xmlStrDoc.loadXML(str); <br>} <br>//ie parse xml string<br>//alert(xmlStrDoc.getElementsByTagName("car")[0].childNodes[0].childNodes[0].childNodes[0].nodeValue);//pop up 500,000<br>//alert(xmlStrDoc.getElementsByTagName("car")[0].childNodes[0].childNodes[1].childNodes[0].nodeValue);//Pop up A6 <br>//Alright Use item(i) to traverse <br>//var strNodes=xmlStrDoc.documentElement.childNodes; <br>//alert(strNodes.item(0).childNodes.item(0).childNodes.item(0).text ); //pop up 500,000 <br>//alert(strNodes.item(0).childNodes.item(1).childNodes.item(0).text); //pop up A6 <br>//Firefox parses xml String <br>//Firefox and IE use textContent to parse the values ​​of different nodes in xml. <br>//And it will add "n" line breaks before and after some hierarchical child nodes. <br>//In other words, the first node is "n", and the second node is the real first node. <br>//The third node is "n", and the fourth node is the real second node. <br>//alert(xmlStrDoc.childNodes[0].childNodes[1].textContent);//pop up 650,000 A8 <br>//alert(xmlStrDoc.childNodes[0].childNodes[1].childNodes[1 ].textContent);//A8 <br>//alert(xmlStrDoc.childNodes[0].childNodes[1].childNodes[0].textContent);//pop up 650,000<br>//Firefox can also use item (1) Function traversal, note that there are also nodes "n" added before and after some hierarchical nodes. <br>//The first node is item(1), the second node is item(3), and the third node is item(5) <br>//var nodes=xmlStrDoc.documentElement.childNodes; <br>//alert(nodes.item(1).textContent); //pop up 650,000 A8 <br>//alert(nodes.item(1).childNodes.item(0).textContent); //pop up 650,000 A8 <br>//alert(nodes.item(1).childNodes.item(1).textContent); //Pop up A8 <br></script>

where xml is for each node The level is the most annoying problem. You can only try it again and again. As long as the correct one comes out,
you can easily determine the hierarchical relationship of the nodes, or debug it.

I feel that json is better to read and understand in this regard. This parsing is too laborious
Copy code The code is as follows:




1.5 million
One bedroom and three bedrooms


2 million


2.3 million



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn