Accessing XML Elements by Name


Accessing XML Elements by Name

We've used properties such as nextSibling and nextChild to navigate through XML documents. However, you also can get individual elements by searching for them by name using methods such as getElementsByTagName .

Here's an example. In this case, I'll do the same thing that we did using properties such as nextSibling and nextChild find the third person's name in Listing 22-01.xml. I'll start by getting a document object for that XML document:

 document1 = new ActiveXObject("Microsoft.XMLDOM")  document1.load("22-01.xml")          .          .          . 

Now we can use getElementsByTagName on this document object. In this case, we want to extract information from the <FIRST_NAME> and <LAST_NAME> elements in the document, so that I get an array of those elements like this:

 document1 = new ActiveXObject("Microsoft.XMLDOM")  document1.load("22-01.xml")  firstNameNodes = document1.getElementsByTagName("FIRST_NAME")   lastNameNodes = document1.getElementsByTagName("LAST_NAME")  .          .          . 

We're looking for data about the third person in the XML document, so that corresponds to element 2 in the 0-based array. And we can do that like this:

 document1 = new ActiveXObject("Microsoft.XMLDOM")  document1.load("22-01.xml")  firstNameNodes = document1.getElementsByTagName("FIRST_NAME")  lastNameNodes = document1.getElementsByTagName("LAST_NAME")  var displayText = "The main guest was: " +   firstNameNodes(2).firstChild.nodeValue + ' '   + lastNameNodes(2).firstChild.nodeValue   div1.innerHTML = displayText  

Here's the whole code:

(Listing 22-05.html on the web site)
 <HTML>      <HEAD>           <TITLE>               Accessing XML Element Values           </TITLE>           <SCRIPT LANGUAGE="JavaScript">                <!--               function reader()                {                    var document1, firstNameNodes, lastNameNodes                    document1 = new ActiveXObject("Microsoft.XMLDOM")                    document1.load("22-01.xml")                    firstNameNodes = document1.getElementsByTagName("FIRST_NAME")                    lastNameNodes = document1.getElementsByTagName("LAST_NAME")                    var displayText = "The main guest was: " +                          firstNameNodes(2).firstChild.nodeValue + ' '                        + lastNameNodes(2).firstChild.nodeValue                    div1.innerHTML = displayText               }               //-->           </SCRIPT>      </HEAD>      <BODY>          <H1>              Accessing XML Element Values          </H1>          <INPUT TYPE="BUTTON" VALUE="Get the name of the main guest"              ONCLICK="reader()">          <BR>          <DIV ID="div1"></DIV>      </BODY>  </HTML> 

This gives the same result we've already seen (see Figure 22.2, for example).

So far, we've seen a number of ways of accessing XML element content, but there's more to the story. What about an XML element's attributes ? I'll take a look at that next .



Inside Javascript
Inside JavaScript
ISBN: 0735712859
EAN: 2147483647
Year: 2005
Pages: 492
Authors: Steve Holzner

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net