XML eXtensible Markup Language (TAG) is widely used in web development as a carrier for data transmission and storage. When we need to pass XML data to the front-end, we need to use javascript to parse it. Therefore, it is also common to use javascript to parse XML. Parse X using javascript
XML eXtensible Markup Language (TAG) is widely used in web development as a carrier for data transmission and storage. When we need to pass XML data to the front-end, we need to use javascript to parse it. Therefore, it is also common to use javascript to parse XML.
Using javascript to parse XML, we can use DOM, a powerful tool (a set of APIS for HTML documents, XML and other documents), that is, using getElementById or getElementsByTagName to parse XML.
If they are suitable for parsing XML, let's consider the following questions:
First, each element node in XML does not necessarily have the id attribute. Second, the two element nodes in XML may have the same id attribute. Therefore, using getElementById will not be able to find the desired unique node. GetElementsByTagName has too many nodes at a time. It is troublesome to retrieve data through traversal.
In XML, compared with JSON, XML files are large and complex in format. It is obvious that it takes more resources and time to parse through DOM, and the user experience is definitely not good.
Is there any better way?
XPATH technology-good medicine for solving problems
What is XPATH? The full name is XML Path Language (XML Path Language), which is suitable for determining the location of a node in the XML document.
We can regard it as a query language similar to SQL, by providing XPATH path information (just like SQL commands) you can find the qualified nodes from the XML query (just like the data needed to be returned from the database ).
In Java, the DOM4j open-source package contains selectSingleNode and selectNodes, which can be used to obtain one or more Nodes Based on XPATH expressions.
In IE6.0 and later versions, we can access XML data in any depth in the same way, which facilitates the parsing of XML data.
However, in firefox and other browsers, the w3c standard XPATH processing method is used, which is not as simple as IE. In firefox, corresponding operation objects and calculation methods need to be created, to return the corresponding node.
Encapsulation Method,Creation can be performed in different browsersHow to encapsulate XPATH operations
// Search all nodes that meet the conditions based on the specified XPATH expression // @ param xmldoc the node that executes the search // @ param sXpath xpath expression function selectNodes (xmldoc, sXpath) {if (window. activeXObject) {// IE browser return xmldoc. selectNodes (sXpath);} else if (window. XPathEvaluator) {// FireFox browser var xpathObj = new XPathEvaluator (); if (xpathObj) {var result = xpathObj. evaluate (sXpath, xmldoc, null, XPathResult. ORDERED_NODE_ITEARTOR_TYPE, null); var nodes = new Array (); var n Ode; while (node = result. iterateNext ())! = Null) {nodes. push (node) ;}return nodes;} else {return null ;}} // find the first node that meets the criteria based on the specified XPATH expression // @ param xmldoc the node that executes the search // @ param sXpath xpath expression function selectSingleNode (xmldoc, sXpath) {if (window. activeXObject) {// IE browser return xmldoc. selectSingleNode (sXpath);} else if (window. XPathEvaluator) {// FireFox browser var xpathObj = new XPathEvaluator (); if (xpathObj) {var result = xpathObj. evaluate (sXpath, xmldoc, null, XPathResult. ORDERED_NODE_ITEARTOR_TYPE, null); return result. singleNodeValue;} else {return null ;}}
Note:
Evaluate ()The method returns a node list. After the node list is obtained, the iterateNext () method traverses all nodes in the list. While ORDERED_NODE_ITERATOR_TYPE is the node list, which is arranged according to the node attributes in the document.
Instance-Get XML data using XPATH expressions
XML
wang
AJAX Professional
35
lee
AJAX In Action
65
zhu
AJAX For Dummies
40
123
Call the selectNodes () function to obtain the values of different parameters.
The following six requirements are described: