js parsing XML files
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 The |
|
JS Parsing xml string
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 The |
|
Test XML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<?xml version= "1.0" encoding= "Utf-8"?> <DongFang> <Company> <cNname>1</cNname> <cip >1</cIP> </Company> <Company> <cNname>2</cNname> <cIP>2</cIP> </ company> <Company> <cNname>3</cNname> <cIP>3</cIP> </Company> <company > <cNname>4</cNname> <cIP>4</cIP> </Company> <Company> <cnname>5</ cnname> <cIP>5</cIP> </Company> <Company> <cNname>6</cNname> <cip>6 </cIP> </Company> </DongFang> |
How to use
?
1 2 3 4 5 6 |
var xmldoc=loadxml (text.xml) var elements = Xmldoc.getelementsbytagname ("Company"); for (var i = 0; i < elements.length i++) {var name = Elements[i].getelementsbytagname ("Cnname") [0].firstchild.nodeval Ue var ip = elements[i].getelementsbytagname ("CIP") [0].firstchild.nodevalue; } |
The above method is suitable for IE, below we discuss the problem of parsing XML under IE and Firefox browser
respectively for IE and Firefox respectively for XML documents and XML string parsing, all the code is commented out, want to see which part of the function,
Just get rid of the annotations.
As for parsing XML in an AJAX environment, the principle is the same, except in Ajax, or parsing the returned XML.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 5 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
<script>//Parsing XML documents/////////////////////////////////////////////////////var Xmldoc=null; //Support IE browser if window. ActiveXObject) {xmldoc=new ActiveXObject ("Microsoft.XMLDOM");}//Support Mozilla browser else if (Document.implementation & & document.implementation.createDocument) {xmldoc = Document.implementation.createDocument (",", null);} else{ Alert ("Here"); } if (xmldoc!=null) {xmldoc.async = false; Xmldoc.load ("House.xml");} //ie and Firefox are not the same as parsers, and the parsing process is different. Following IE parses XML document//alert (Xmldoc.getelementsbytagname ("address") [0].childnodes[0].childnodes[0].childnodes[0]. NodeValue)//pop-up 1.5 million//alert (Xmldoc.getelementsbytagname ("address") [0].childnodes[0].childnodes[1].childnodes[0] NodeValue)//pop-up one room triple //Layer traversal Analysis Childnodes[1]//alert (xmldoc.childnodes[1].childnodes[1].childnodes[0). Childnodes[0].nodevalue);/Eject 2 million//alert (Xmldoc.childnodes[1].childnodes[0].childnodes[0].childnodes[0). NodeValue);/eject 1.5 million//alert (Xmldoc.childnodes[1].childnodes[0].childnodes[1].childnodes[0].nodevaLue)//pop-up room three //can also use item (i) to traverse//var nodes=xmldoc.documentelement.childnodes; Alert (Nodes.item (0). Childnodes.item (0). Childnodes.item (0). Text); Eject 1.5 million//alert (Nodes.item (0). Childnodes.item (1). Childnodes.item (0). Text); Pop-up one room triple //Firefox parse XML document//Firefox browser and IE parse XML node value with Textcontent. And he's going to add "n" line breaks before and after the child nodes in the hierarchy. (This is not clear why, with the Firebug debugging is the case, so the written code is best to test, change the environment is wrong)//That is, the 1th node is "n", the 2nd node is the real first node. The 3rd node is "n", and the 4th node is the true second node. Layer get parse Childnodes[0]//alert (xmldoc.childnodes[0].childnodes[1].childnodes[1].textcontent);//Eject 1.5 million//alert ( xmldoc.childnodes[0].childnodes[1].childnodes[3].textcontent)//pop-up one room triple //Direct Access Node name resolution getElementsByTagName (" Address ")//alert (Xmldoc.getelementsbytagname (" address ") [0].textcontent);/pop-up 1,500,001 room three 20.003 billion//alert ( Xmldoc.getelementsbytagname ("Address") [0].childnodes[1].textcontent);/pop up 1,500,001 room three-Habitat//alert ( Xmldoc.getelementsbytagname ("Address") [0].childnodes[1].childnodes[1].textcontent);//Eject 1.5 million//alert ( Xmldoc.getelementsbytagname ("Address") [0].childnodes[1].childnodes[3].textcontent)//pop up one room three-Habitat//alert (xmldoc.getelementsbytagname ("address") [0].childnodes[3] textcontent)//pop-up 2 million //Firefox can also be traversed using the item (1) function, note that there are also some hierarchy node before and after the node "n". The first node is item (1), the second node is item (3), and the third node is Item (5)//item (1) Function traversal parsing//var nodes=xmldoc.documentelement.childnodes; Alert (Nodes.item (1). textcontent); Pop up 1,500,001 room triple//alert (Nodes.item (1). Childnodes.item (1). textcontent); Eject 1.5 million//alert (Nodes.item (1). Childnodes.item (3). Textcontent); One room triple //parse XML string/////////////////////////////////////////////////////////////////////////var str= "<car > "+" <brand><price>50 </price><pattern>A6</pattern></brand> "+" <brand ><PRICE>65 million </price><pattern>A8</pattern></brand> "+" <brand><price> 170,000 </price></brand> "+" </car> "; //Cross-browser, the parser used by IE and Firefox to parse XML is different. var xmlstrdoc=null; if (window. Domparser) {//Mozilla Explorer parser=new domparser (); xmlstrdoc=parser.parsefromstring(str, "text/xml"); }else{//Internet Explorer xmlstrdoc=new activexobject ("Microsoft.XMLDOM"); xmlstrdoc.async= "false"; Xmlstrdoc.loadxml (str); } //ie Parse XML string//alert (Xmlstrdoc.getelementsbytagname ("Car") [0].childnodes[0].childnodes[0].childnodes[0]. NodeValue);//Eject 500,000//alert (Xmlstrdoc.getelementsbytagname ("Car") [0].childnodes[0].childnodes[1].childnodes[0]. NodeValue);//Eject A6 //also can traverse//var strnodes=xmlstrdoc.documentelement.childnodes with item (i); Alert (Strnodes.item (0). Childnodes.item (0). Childnodes.item (0). Text); Eject 500,000//alert (Strnodes.item (0). Childnodes.item (1). Childnodes.item (0). Text); Pop-up A6 //Firefox parse XML string//Firefox browser and IE parse XML differently node values with Textcontent. And he's going to add "n" line breaks before and after the child nodes in the hierarchy. That means the 1th node is "n", and the 2nd node is the real first node. The 3rd node is "n", and the 4th node is the true second node. alert (xmlstrdoc.childnodes[0].childnodes[1].textcontent);//Eject 650,000 A8//alert (xmlstrdoc.childnodes[0].childnodes [1].childnodes[1].textcontent);//a8//alert (xmlstrdoc.childnodes[0].childnodes[1].childnodes[0].textcontent); Pop Up 650,000 Firefox can also use the item (1) Function traversal, note that there are also some hierarchy node before and after the node "n". The first node is item (1), the second node is item (3), and the third node is Item (5)//var nodes=xmlstrdoc.documentelement.childnodes; Alert (Nodes.item (1). textcontent); Pop-up 650,000 A8//alert (Nodes.item (1). Childnodes.item (0). textcontent); Eject 650,000//alert (Nodes.item (1). Childnodes.item (1). textcontent); Pop-up A8 </script> |
Where the XML each node level is the most annoying problem, can only try again and again, just come out a right,
It's a good way to determine the level of a node, or debug it.
The sense that JSON is still better read and understand. This parsing is too strenuous!
The document House.xml content is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13-14 |
<?xml version= "1.0" encoding= "Utf-8"?> <address> <city name= "Beijing" > <price>150 million </price> <type> one room triple </type> </city> <city name= "Shanghai" > <price>200 </price> </city> <city name= "Hangzhou" > <price>230 million </price> </city> <city name= "Nanjing" ></city> </ Address> |