JS parse XML string and XML document implementation principle and code (for IE and Firefox) _javascript skills

Source: Internet
Author: User
respectively for IE and Firefox respectively for the XML document and XML string parsing, all the code commented out, want to see which part of the function, remove the annotation can be.

As for parsing XML in an AJAX environment, the principle is the same, except in Ajax, or parsing the returned XML.
Copy Code code as follows:

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
<title> New Document </title>
<meta name= "Author" content= "Lushuaiyin" >
<body>
Parsing XML documents and XML strings with JS
</body>
<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 the parser, the parsing process is different. Following
IE parsing XML documents
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 a room, three lives.
Layer Traversal parsing 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 one room three lives
You can also use item (i) to traverse
var nodes=xmldoc.documentelement.childnodes;
Alert (Nodes.item (0). Childnodes.item (0). Childnodes.item (0). Text); Pop up 1.5 million
Alert (Nodes.item (0). Childnodes.item (1). Childnodes.item (0). Text); Pop up a room, three lives.
Firefox parsing XML document
Firefox browser and IE parse XML not the same value of nodes with Textcontent.
And he would put "\ n" line breaks before and after the child nodes of the hierarchy. (This is not clear why, with Firebug debugging is this appearance, so the code has been written best test, change the environment is wrong)
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.
Layer-by-layer fetch parsing 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 three lives
Direct Access Node name resolution getElementsByTagName ("address")
Alert (Xmldoc.getelementsbytagname ("address") [0].textcontent);/pop 1,500,001 room three 20.003 billion
Alert (Xmldoc.getelementsbytagname ("address") [0].childnodes[1].textcontent);/pop 1,500,001 room three-bedroom
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 lives
Alert (Xmldoc.getelementsbytagname ("address") [0].childnodes[3].textcontent);//Eject 2 million
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)
Item (1) Function traversal parsing
var nodes=xmldoc.documentelement.childnodes;
Alert (Nodes.item (1). textcontent); Pop up 1,500,001 room three habitat
Alert (Nodes.item (1). Childnodes.item (1). textcontent); Pop up 1.5 million
Alert (Nodes.item (1). Childnodes.item (3). Textcontent); One Bedroom Three lives
Parsing XML string/////////////////////////////////////////////////////////////////////////
var str= "<car>" +
"<brand><price>50 million </price><pattern>A6</pattern></brand>" +
"<brand><price>65 million </price><pattern>A8</pattern></brand>" +
"<brand><price>17 million </price></brand>" +
"</car>";
Cross-browsers, IE and Firefox parse XML using the parser is not the same.
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 parsing XML strings
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
You can also use item (i) to traverse
var strnodes=xmlstrdoc.documentelement.childnodes;
Alert (Strnodes.item (0). Childnodes.item (0). Childnodes.item (0). Text); Pop up 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 not the same value of nodes with Textcontent.
And he would put "\ n" line breaks before and after the child nodes of 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);//Eject 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); Pop Up 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.
Copy Code code as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
<address>
<city name= "Beijing" >
<price>150 million </price>
<type> a three-bedroom </type>
</city>
<city name= "Shanghai" >
<PRICE>200 million </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.