Javascript traverse xml to find the value of the specified node

Lionsure 2020-05-28 Original by the website

XML is more and more widely used. It is a very common operation to use javascript to traverse and take the value of the specified node in the xml file. There are two commonly got methods, one is to get in order with the childNodes attribute; the other is to get with for (or foreach) loop. In order to be more intuitive, the article first lists an xml file of the specified node to be got, and then uses two methods to get it.

 

For example, the xml file is as follows:

<?xml version="1.0" encoding="utf-8"?>
       <root>
              <employee>
                     <name>Jioken</name>
                     <sex>Female</sex>
                     <birthday>1982-12-27</birthday>
              </employee>
              <employee>
                     <name>Nenaly</name>
                     <sex>Female</sex>
                     <birthday>1986-9-8</birthday>
              </employee>
              <employee>
                     <name>Bureter</name>
                     <sex>Male</sex>
                     <birthday>1984-8-19</birthday>
              </employee>
       </root>

What should I do if I want to get the birthday of employee named "Nenaly"?

 

1. Javascript traverse xml to find the value of the specified node in order with childNodes

Implementation process: First create an xml object, then load the xml file, and then determine the position of node to be fetched according to the sequence of the parent node and node to be fetched in xml, and finally return the value of node to be fetched.

//pId is the serial number of the parent node of node to be fetched
       //cId is the serial number of node to be fetched
       function getlNodeValueInXml(pId, cId) {
              var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
              xmlDoc.async = false;

       xmlDoc.load( "employeeInfo.xml");
              var nodes = xmlDoc.documentElement.childNodes[pId].childNodes[cId];
              return nodes.childNodes[0].text;
       }

Call: getlNodeValueInXml(1, 2);

 

2. Javascript traverse xml to find the value of the specified node with for loop

Implementation process: first create an xml object supported by ie, if an exception occurs, create an empty xml object supported by FireFox and Chrome and return empty; then load the xml file and return empty if an exception occurs; finally, traverse to find the node with the same value as the incoming node with the for loop; when found, it returns the attribute value belonging to the node.

//nodeValue is the value of node to be fetched
       function getlNodeValueInXmlWithFor(nodeValue)
       {
              var xmlDoc;
              try {
                     //Create an XML document object supported by ie
                     xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
              }
              catch (e)
              {
                     try {
                            //Create an XML document object supported by FireFox and Chrome
                            xmlDoc = document.implementation.createDocument("", "", null);

              } catch (e) {
                            alert(e.message);
                            return "";
                     }
              }

       xmlDoc.async = false;

              try {
                     xmlDoc.load("employeeInfo.xml");
              }
              catch (e) {
                     alert(e.message);
                     return "";
              }

       var xd = xmlDoc.documentElement.childNodes;
              if (xd == null) return "";
              var tempValue;

       for (var i = 0; i < xd.length; i++) {
                     if (xd[i].childNodes[0].childNodes[0].nodeValue == nodeValue)
                            tempValue = xd[i].childNodes[2].childNodes[0].nodeValue;
              }
              return tempValue;
       }

Call: getlNodeValueInXmlWithFor("Nenaly");