<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 = "<car>" + "<Brand> <price> 0.5 million </price> <pattern> A6 </pattern> </brand>" + "<Brand> <price> 0.65 million </price> <pattern> A8 </pattern> </brand>" + "<Brand> <price> 0.17 million </price> </brand>" + "</Car> "; // 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> |