O'Reilly logo

Real World XML by Steven Holzner

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Getting Elements by Name

So far in this chapter, I've used navigation methods such as nextSibling and nextChild to move through XML documents. However, you can also get individual elements by searching for them by name. Here's an example. In this case, I'll use the document object's getElementsByTagName method to return a node list object holding all elements of a given name. In particular, I'm searching for <FIRST_NAME> and <LAST_NAME> elements, so I get lists of those elements like this:

<HTML> 
    <HEAD>
         <TITLE>
             Reading XML element values
         </TITLE>

         <SCRIPT LANGUAGE="JavaScript">
              function loadDocument()
              {
                  var xmldoc, listNodesFirstName, listNodesLastName

                  xmldoc = new ActiveXObject("Microsoft.XMLDOM")
                  xmldoc.load("ch07_01.xml")

                  listNodesFirstName ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required