


js parsing xml string and xml document implementation principle and code (for ie and firefox)_javascript skills
We have parsed XML documents and XML strings for IE and Firefox respectively. All codes are commented out. If you want to see some functions, just remove the comments.
As for parsing xml in an ajax environment, the principle is actually the same, except that if it is placed in ajax, the returned xml still needs to be parsed.
head>
Use js to parse xml documents and xml strings
<script> <br>//Parse xml document//////////////////////////////////////////////////// ///// <br>var xmlDoc=null; <br>//Support IE browser <br>if(window.ActiveXObject){ <br>xmlDoc=new ActiveXObject("Microsoft.XMLDOM"); <br> } <br>//Support Mozilla browser<br>else if(document.implementation && document.implementation.createDocument){ <br>xmlDoc = document.implementation.createDocument('','',null); <br> } <br>else{ <br>alert("here"); <br>} <br>if(xmlDoc!=null){ <br>xmlDoc.async = false; <br>xmlDoc.load("house. xml"); <br>} <br>//IE and Firefox not only have different parsers, but also different parsing processes. As follows; <br>//ie parse xml document<br>//alert(xmlDoc.getElementsByTagName("address")[0].childNodes[0].childNodes[0].childNodes[0].nodeValue);// Pop up 1.5 million<br>//alert(xmlDoc.getElementsByTagName("address")[0].childNodes[0].childNodes[1].childNodes[0].nodeValue);//pop up one bedroom and three bedrooms<br> //Traverse and parse childNodes[1] <br>//alert(xmlDoc.childNodes[1].childNodes[1].childNodes[0].childNodes[0].nodeValue);//pop up 2 million <br> //alert(xmlDoc.childNodes[1].childNodes[0].childNodes[0].childNodes[0].nodeValue); //1.5 million pops up<br>//alert(xmlDoc.childNodes[1].childNodes[ 0].childNodes[1].childNodes[0].nodeValue);//Pop up one room and three bedrooms<br>//You can also use item(i) to traverse<br>//var nodes=xmlDoc.documentElement.childNodes ; <br>//alert(nodes.item(0).childNodes.item(0).childNodes.item(0).text); //1.5 million pops up<br>//alert(nodes.item(0) .childNodes.item(1).childNodes.item(0).text); //Pop up one room and three bedrooms<br>//Firefox parses xml document<br>//Firefox and IE parse xml for different nodes Use textContent for the value. <br>//And it will add "n" line breaks before and after some hierarchical child nodes. (I don’t know why. It looks like this when debugging with firebug, so it is best to test the code you have written. It will not be correct in another environment) <br>//In other words, the first node is "n", and the first node is "n". 2 nodes are the real first node. <br>//The third node is "n", and the fourth node is the real second node. <br>//Get and parse childNodes[0] layer by layer <br>//alert(xmlDoc.childNodes[0].childNodes[1].childNodes[1].textContent);//pop up 1.5 million<br>// alert(xmlDoc.childNodes[0].childNodes[1].childNodes[3].textContent);//Pop up a three-bedroom apartment<br>//Get the node name resolution directly getElementsByTagName("address") <br>// alert(xmlDoc.getElementsByTagName("address")[0].textContent);//Pop up 1.5 million for one bedroom and three bedrooms for 2 million and 3 million <br>//alert(xmlDoc.getElementsByTagName("address")[0].childNodes [1].textContent);//Pop up 1.5 million for one room and three bedrooms<br>//alert(xmlDoc.getElementsByTagName("address")[0].childNodes[1].childNodes[1].textContent);// Pop up 1.5 million<br>//alert(xmlDoc.getElementsByTagName("address")[0].childNodes[1].childNodes[3].textContent);//Pop up one room and three bedrooms<br>//alert(xmlDoc .getElementsByTagName("address")[0].childNodes[3].textContent);//pop up 2 million <br>//Firefox can also use the item(1) function to traverse. Note that some hierarchical nodes are also added before and after Node "n". <br>//The first node is item(1), the second node is item(3), and the third node is item(5) <br>//item(1) function traversal analysis<br>/ /var nodes=xmlDoc.documentElement.childNodes; <br>//alert(nodes.item(1).textContent); //pop up 1.5 million one-bedroom, three-bedroom <br>//alert(nodes.item(1). childNodes.item(1).textContent); //1.5 million pops up<br>//alert(nodes.item(1).childNodes.item(3).textContent); //One bedroom and three bedrooms<br>// Parse xml string ////////////////////////////////////////////////// //////////////////////////// <br>var str="<car>" <br>"<brand><price> ;500,000</price><pattern>A6</pattern></brand>" <br>"<brand><price>650,000</price><pattern>A8</pattern> ;</brand>" <br>"<brand><price>170,000</price></brand>" <br>"</car>"; <br>//Cross-browse The parsers used by IE and Firefox to parse xml are different.<br>var xmlStrDoc=null; <br>if (window.DOMParser){// Mozilla Explorer <br>parser=new DOMParser(); <br>xmlStrDoc=parser.parseFromString(str,"text/xml"); <br>}else{// Internet Explorer <br>xmlStrDoc=new ActiveXObject("Microsoft.XMLDOM"); <br>xmlStrDoc.async="false"; <br>xmlStrDoc.loadXML(str); <br>} <br>//ie parse xml string<br>//alert(xmlStrDoc.getElementsByTagName("car")[0].childNodes[0].childNodes[0].childNodes[0].nodeValue);//pop up 500,000<br>//alert(xmlStrDoc.getElementsByTagName("car")[0].childNodes[0].childNodes[1].childNodes[0].nodeValue);//Pop up A6 <br>//Alright Use item(i) to traverse <br>//var strNodes=xmlStrDoc.documentElement.childNodes; <br>//alert(strNodes.item(0).childNodes.item(0).childNodes.item(0).text ); //pop up 500,000 <br>//alert(strNodes.item(0).childNodes.item(1).childNodes.item(0).text); //pop up A6 <br>//Firefox parses xml String <br>//Firefox and IE use textContent to parse the values of different nodes in xml. <br>//And it will add "n" line breaks before and after some hierarchical child nodes. <br>//In other words, the first node is "n", and the second node is the real first node. <br>//The third node is "n", and the fourth node is the real second node. <br>//alert(xmlStrDoc.childNodes[0].childNodes[1].textContent);//pop up 650,000 A8 <br>//alert(xmlStrDoc.childNodes[0].childNodes[1].childNodes[1 ].textContent);//A8 <br>//alert(xmlStrDoc.childNodes[0].childNodes[1].childNodes[0].textContent);//pop up 650,000<br>//Firefox can also use item (1) Function traversal, note that there are also nodes "n" added before and after some hierarchical nodes. <br>//The first node is item(1), the second node is item(3), and the third node is item(5) <br>//var nodes=xmlStrDoc.documentElement.childNodes; <br>//alert(nodes.item(1).textContent); //pop up 650,000 A8 <br>//alert(nodes.item(1).childNodes.item(0).textContent); //pop up 650,000 A8 <br>//alert(nodes.item(1).childNodes.item(1).textContent); //Pop up A8 <br></script>
where xml is for each node The level is the most annoying problem. You can only try it again and again. As long as the correct one comes out,
you can easily determine the hierarchical relationship of the nodes, or debug it.
I feel that json is better to read and understand in this regard. This parsing is too laborious

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



In-depth analysis of the role and application scenarios of HTTP status code 460 HTTP status code is a very important part of web development and is used to indicate the communication status between the client and the server. Among them, HTTP status code 460 is a relatively special status code. This article will deeply analyze its role and application scenarios. Definition of HTTP status code 460 The specific definition of HTTP status code 460 is "ClientClosedRequest", which means that the client closes the request. This status code is mainly used to indicate

iBatis and MyBatis: Differences and Advantages Analysis Introduction: In Java development, persistence is a common requirement, and iBatis and MyBatis are two widely used persistence frameworks. While they have many similarities, there are also some key differences and advantages. This article will provide readers with a more comprehensive understanding through a detailed analysis of the features, usage, and sample code of these two frameworks. 1. iBatis features: iBatis is an older persistence framework that uses SQL mapping files.

Detailed explanation of Oracle error 3114: How to solve it quickly, specific code examples are needed. During the development and management of Oracle database, we often encounter various errors, among which error 3114 is a relatively common problem. Error 3114 usually indicates a problem with the database connection, which may be caused by network failure, database service stop, or incorrect connection string settings. This article will explain in detail the cause of error 3114 and how to quickly solve this problem, and attach the specific code

[Analysis of the meaning and usage of midpoint in PHP] In PHP, midpoint (.) is a commonly used operator used to connect two strings or properties or methods of objects. In this article, we’ll take a deep dive into the meaning and usage of midpoints in PHP, illustrating them with concrete code examples. 1. Connect string midpoint operator. The most common usage in PHP is to connect two strings. By placing . between two strings, you can splice them together to form a new string. $string1=&qu

Wormhole is a leader in blockchain interoperability, focused on creating resilient, future-proof decentralized systems that prioritize ownership, control, and permissionless innovation. The foundation of this vision is a commitment to technical expertise, ethical principles, and community alignment to redefine the interoperability landscape with simplicity, clarity, and a broad suite of multi-chain solutions. With the rise of zero-knowledge proofs, scaling solutions, and feature-rich token standards, blockchains are becoming more powerful and interoperability is becoming increasingly important. In this innovative application environment, novel governance systems and practical capabilities bring unprecedented opportunities to assets across the network. Protocol builders are now grappling with how to operate in this emerging multi-chain

Analysis of new features of Win11: How to skip logging in to a Microsoft account. With the release of Windows 11, many users have found that it brings more convenience and new features. However, some users may not like having their system tied to a Microsoft account and wish to skip this step. This article will introduce some methods to help users skip logging in to a Microsoft account in Windows 11 and achieve a more private and autonomous experience. First, let’s understand why some users are reluctant to log in to their Microsoft account. On the one hand, some users worry that they

Detailed analysis and examples of exponential functions in C language Introduction: The exponential function is a common mathematical function, and there are corresponding exponential function library functions that can be used in C language. This article will analyze in detail the use of exponential functions in C language, including function prototypes, parameters, return values, etc.; and give specific code examples so that readers can better understand and use exponential functions. Text: The exponential function library function math.h in C language contains many functions related to exponentials, the most commonly used of which is the exp function. The prototype of exp function is as follows

Due to space limitations, the following is a brief article: Apache2 is a commonly used web server software, and PHP is a widely used server-side scripting language. In the process of building a website, sometimes you encounter the problem that Apache2 cannot correctly parse the PHP file, causing the PHP code to fail to execute. This problem is usually caused by Apache2 not configuring the PHP module correctly, or the PHP module being incompatible with the version of Apache2. There are generally two ways to solve this problem, one is
