How javascript parses xml strings and xml documents and code (for ie and Firefox) _ javascript skills

Source: Internet
Author: User
Parse xml documents and xml strings for ie and Firefox respectively, taking into account the compatibility of the browser, as for parsing xml in the ajax environment, the principle is the same, I just put it in ajax, but I still want to parse the returned xml. If you are interested, you can understand it, it may be helpful for you to learn about js parsing xml, and parse the xml document and xml string for ie and Firefox respectively. All the code has been commented out. Which function do you want to see, just remove the comment.

As for xml parsing in the ajax environment, the principle is the same, but the xml returned must be parsed In the ajax environment.

The Code is as follows:





New Document



Parse xml documents and xml strings using js


Script
// Parse the xml document /////////////////////////////////// //////////////////
Var xmlDoc = null;
// Supports Internet Explorer
If (window. ActiveXObject ){
XmlDoc = new ActiveXObject ("Microsoft. XMLDOM ");
}
// Supports Mozilla browsers
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 only different in the parser, but also in the parsing process. As follows;
// Parse xml document with ie
// Alert (xmlDoc. getElementsByTagName ("address") [0]. childNodes [0]. childNodes [0]. childNodes [0]. nodeValue); // 1.5 million is displayed.
// Alert (xmlDoc. getElementsByTagName ("address") [0]. childNodes [0]. childNodes [1]. childNodes [0]. nodeValue); // a three-bedroom apartment is displayed.
// Layer-by-layer traversal and parsing childNodes [1]
// Alert (xmlDoc. childNodes [1]. childNodes [1]. childNodes [0]. childNodes [0]. nodeValue); // the 2 million
// Alert (xmlDoc. childNodes [1]. childNodes [0]. childNodes [0]. childNodes [0]. nodeValue); // The 1.5 million
// Alert (xmlDoc. childNodes [1]. childNodes [0]. childNodes [1]. childNodes [0]. nodeValue); // The first room and three residences are displayed.
// You can also use item (I) For traversal.
// Var nodesstrap xmldoc.doc umentElement. childNodes;
// Alert (nodes. item (0). childNodes. item (0). childNodes. item (0). text); // 1.5 million is displayed
// Alert (nodes. item (0). childNodes. item (1). childNodes. item (0). text); // The first room and three residences are displayed.
// Firefox parses xml documents
// Firefox and ie use textContent to parse different xml node values.
// And it will add the "\ n" line break before and after some child nodes. (I don't know why. It looks like this when debugging with firebug, so it is best to test the code that has been written. It is wrong to change the environment)
// That is to say, 1st nodes are \ n, and 2nd nodes are the real first node.
// 3rd nodes are \ n, and 4th nodes are the real second node.
// Retrieve and parse childNodes through layers [0]
// Alert (xmlDoc. childNodes [0]. childNodes [1]. childNodes [1]. textContent); // 1.5 million is displayed
// Alert (xmlDoc. childNodes [0]. childNodes [1]. childNodes [3]. textContent); // The first room and three residences are displayed.
// Directly retrieve the node name and parse getElementsByTagName ("address ")
// Alert (xmlDoc. getElementsByTagName ("address") [0]. textContent); // The first room, third bedroom, 1.5 million, 2 million, 3 million, is displayed.
// Alert (xmlDoc. getElementsByTagName ("address") [0]. childNodes [1]. textContent); // The room 1.5 million, Room 1, and three residences are displayed.
// Alert (xmlDoc. getElementsByTagName ("address") [0]. childNodes [1]. childNodes [1]. textContent); // The 1.5 million
// Alert (xmlDoc. getElementsByTagName ("address") [0]. childNodes [1]. childNodes [3]. textContent); // The first room and three residences are displayed.
// Alert (xmlDoc. getElementsByTagName ("address") [0]. childNodes [3]. textContent); // the 2 million
// Firefox can also be traversed using the item (1) function. Note that node "\ n" is added before and after some hierarchical nodes ".
// 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 nodesstrap xmldoc.doc umentElement. childNodes;
// Alert (nodes. item (1). textContent); // pop up room 1.5 million, three-bedroom
// Alert (nodes. item (1). childNodes. item (1). textContent); // The 1.5 million
// Alert (nodes. item (1). childNodes. item (3). textContent); // three-bedroom
// Parse the xml string /////////////////////////////////// //////////////////////////////////////
Var str =" "+
" 0.5 million A6 "+
" 0.65 million A8 "+
" 0.17 million "+
" ";
// Cross-browser, the parser used by ie and Firefox for parsing 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 );
}
// Parse xml strings with ie
// Alert (xmlStrDoc. getElementsByTagName ("car") [0]. childNodes [0]. childNodes [0]. childNodes [0]. nodeValue); // 0.5 million is displayed.
// Alert (xmlStrDoc. getElementsByTagName ("car") [0]. childNodes [0]. childNodes [1]. childNodes [0]. nodeValue); // the A6 is displayed.
// You can also use item (I) For traversal.
// Var strnodesstrap xmlstrdoc.doc umentElement. childNodes;
// Alert (strNodes. item (0). childNodes. item (0). childNodes. item (0). text); // 0.5 million is displayed
// Alert (strNodes. item (0). childNodes. item (1). childNodes. item (0). text); // A6.
// Firefox parses xml strings
// Firefox and ie use textContent to parse different xml node values.
// And it will add the "\ n" line break before and after some child nodes.
// That is to say, 1st nodes are \ n, and 2nd nodes are the real first node.
// 3rd nodes are \ n, and 4th nodes are the real second node.
// Alert (xmlStrDoc. childNodes [0]. childNodes [1]. textContent); // The 0.65 million A8
// Alert (xmlStrDoc. childNodes [0]. childNodes [1]. childNodes [1]. textContent); // A8
// Alert (xmlStrDoc. childNodes [0]. childNodes [1]. childNodes [0]. textContent); // 0.65 million is displayed
// Firefox can also be traversed using the item (1) function. Note that node "\ n" is added before and after some hierarchical nodes ".
// The first node is item (1), the second node is item (3), and the third node is item (5)
// Var nodesstrap xmlstrdoc.doc umentElement. childNodes;
// Alert (nodes. item (1). textContent); // 0.65 million A8 is displayed.
// Alert (nodes. item (1). childNodes. item (0). textContent); // The 0.65 million
// Alert (nodes. item (1). childNodes. item (1). textContent); // the A8
Script


The layer of each xml node is the most annoying problem. You can only try it again and again,
Determine the node hierarchy, or debug it.

I feel that json is better able to read and understand this aspect. This parsing is too hard

The Code is as follows:





1.5 million
One room, three residences


2 million


2.3 million


Related Article

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.