JS parse XML file and XML string

Source: Internet
Author: User

 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 &lt;script&gt;//Parsing XML documents/////////////////////////////////////////////////////var Xmldoc=null; &nbsp;//Support IE browser if window. ActiveXObject) {xmldoc=new ActiveXObject ("Microsoft.XMLDOM");}//Support Mozilla browser else if (Document.implementation &amp; &amp; document.implementation.createDocument) {xmldoc = Document.implementation.createDocument (",", null);} else{ Alert ("Here"); } if (xmldoc!=null) {xmldoc.async = false; Xmldoc.load ("House.xml");} &nbsp;//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 &nbsp;//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 &nbsp;//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 &nbsp;//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 &nbsp;//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 &nbsp;//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 &nbsp;//parse XML string/////////////////////////////////////////////////////////////////////////var str= "&lt;car &gt; "+" &lt;brand&gt;&lt;price&gt;50 &lt;/price&gt;&lt;pattern&gt;A6&lt;/pattern&gt;&lt;/brand&gt; "+" &lt;brand &GT;&LT;PRICE&GT;65 million &lt;/price&gt;&lt;pattern&gt;A8&lt;/pattern&gt;&lt;/brand&gt; "+" &lt;brand&gt;&lt;price&gt; 170,000 &lt;/price&gt;&lt;/brand&gt; "+" &lt;/car&gt; "; &nbsp;//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); } &nbsp;//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 &nbsp;//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 &nbsp;//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 &nbsp; 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 &nbsp; &lt;/script&gt;

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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.