Accessing Attribute Values in XML Elements


Like HTML elements, XML elements can have attributes. To show how to read attribute values from an XML document, I'll create a new example to read the value of the ATTENDENCE attribute of the third person in the XML document 22-01.xml:

 <?xml version="1.0"?>  <EVENTS>      <EVENT TYPE="informal">         <EVENT_TITLE>15th Award Ceremony</EVENT_TITLE>         <EVENT_NUMBER>1207</EVENT_NUMBER>         <SUBJECT>Gala Event</SUBJECT>         <DATE>7/4/2003</DATE>         <PEOPLE>             <PERSON ATTENDENCE="present">                 <FIRST_NAME>Sam</FIRST_NAME>                 <LAST_NAME>Edwards</LAST_NAME>             </PERSON>             <PERSON ATTENDENCE="absent">                 <FIRST_NAME>Sally</FIRST_NAME>                 <LAST_NAME>Jackson</LAST_NAME>             </PERSON>  <PERSON ATTENDENCE="present">  <FIRST_NAME>Cary</FIRST_NAME>                 <LAST_NAME>Grant</LAST_NAME>             </PERSON>         .         .         . 

How do you read attribute values? You start by getting the attributes collection of the current element using that element's attributes property. In this case, we want the attributes of the third <PERSON> element, and we get the attributes collection like this by navigating to that element:

 var document1, eventsNode, eventNode, peopleNode  var firstNameNode, lastNameNode, displayText  var attributes, attendencePerson  document1= document.all("xml1").XMLDocument  eventsNode = document1.documentElement  eventNode = eventsNode.firstChild  peopleNode = eventNode.lastChild  personNode = peopleNode.lastChild  firstNameNode = personNode.firstChild  lastNameNode = firstNameNode.nextSibling  attributes = personNode.attributes  .          .          . 

Now we can get the value of the ATTENDENCE attribute with the attribute collection's getNamedItem method, passing it the name of the attribute. This returns, not the value of the attribute as you might hope, but an attribute object, and you must use that object's value property to get the value of the attribute:

 var document1, eventsNode, eventNode, peopleNode  var firstNameNode, lastNameNode, displayText  var attributes, attendencePerson  document1= document.all("xml1").XMLDocument  eventsNode = document1.documentElement  eventNode = eventsNode.firstChild  peopleNode = eventNode.lastChild  personNode = peopleNode.lastChild  firstNameNode = personNode.firstChild  lastNameNode = firstNameNode.nextSibling  attributes = personNode.attributes  attendencePerson = attributes.getNamedItem("ATTENDENCE")   displayText = firstNameNode.firstChild.nodeValue   + ' ' + lastNameNode.firstChild.nodeValue   + " was " + attendencePerson.value   div1.innerHTML=displayText  

Here's what the entire code looks like:

(Listing 22-06.html on the web site)
 <HTML>      <HEAD>          <TITLE>              Reading XML Element Attribute Values          </TITLE>          <XML ID="xml1" SRC="22-01.xml"></XML>          <SCRIPT LANGUAGE="JavaScript">              <!--             function readXMLDocument()              {                   var document1, eventsNode, eventNode, peopleNode                   var firstNameNode, lastNameNode, displayText                   var attributes, attendencePerson                     document1= document.all("xml1").XMLDocument                    eventsNode = document1.documentElement                    eventNode = eventsNode.firstChild                    peopleNode = eventNode.lastChild                    personNode = peopleNode.lastChild                    firstNameNode = personNode.firstChild                    lastNameNode = firstNameNode.nextSibling                    attributes = personNode.attributes                    attendencePerson = attributes.getNamedItem("ATTENDENCE")                    displayText = firstNameNode.firstChild.nodeValue                        + ' ' + lastNameNode.firstChild.nodeValue                        + " was " + attendencePerson.value                    div1.innerHTML=displayText               }               //-->           </SCRIPT>      </HEAD>      <BODY>          <H1>              Reading XML Element Attribute Values          </H1>          <INPUT TYPE="BUTTON" VALUE="Get Attendence of the Main Guest"              ONCLICK="readXMLDocument()">          <BR>          <DIV ID="div1"></DIV>      </BODY>  </HTML> 

You can see the results in Figure 22.3, where we're displaying an attribute's value.

Figure 22.3. Displaying attribute values from an XML document.

graphics/22fig03.gif



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