Getting Attribute Values from XML Elements

To show you how to read attribute values from an XML document, I'll read the value of the ATTENDANCE attribute of the third person in the XML document ch07_01.xml:

 <?xml version="1.0"?>  <MEETINGS>    <MEETING TYPE="informal">        <MEETING_TITLE>XML In The Real World</MEETING_TITLE>        <MEETING_NUMBER>2079</MEETING_NUMBER>        <SUBJECT>XML</SUBJECT>        <DATE>6/1/2003</DATE>        <PEOPLE>            <PERSON ATTENDANCE="present">                <FIRST_NAME>Edward</FIRST_NAME>                <LAST_NAME>Samson</LAST_NAME>            </PERSON>            <PERSON ATTENDANCE="absent">                <FIRST_NAME>Ernestine</FIRST_NAME>                <LAST_NAME>Johnson</LAST_NAME>            </PERSON>  <PERSON ATTENDANCE="present">  <FIRST_NAME>Betty</FIRST_NAME>                <LAST_NAME>Richardson</LAST_NAME>            </PERSON>        </PEOPLE>    </MEETING> </MEETINGS> 

How do you read attribute values? You start by getting a named node map object of the attributes 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 a named node map of those attributes like this:

 <HTML>      <HEAD>          <TITLE>              Reading attribute values from XML documents          </TITLE>          <XML ID="meetingsXML" SRC="ch07_01.xml"></XML>          <SCRIPT LANGUAGE="JavaScript">              function readXMLDocument()              {                   var xmldoc, meetingsNode, meetingNode, peopleNode                   var first_nameNode, last_nameNode, outputText                   var attributes                   xmldoc= document.all("meetingsXML").XMLDocument                   meetingsNode = xmldoc.documentElement                   meetingNode = meetingsNode.firstChild                   peopleNode = meetingNode.lastChild                   personNode = peopleNode.lastChild                   first_nameNode = personNode.firstChild                   last_nameNode = first_nameNode.nextSibling  attributes = personNode.attributes  .                   .                   . </HTML> 

Now I can recover the actual node for the ATTENDANCE node with the named node map object's getNamedItem method:

 attributes = personNode.attributes  attendancePerson = attributes.getNamedItem("ATTENDANCE")  .                   .                   . 

Now I have a node corresponding to the ATTENDANCE attribute, and I can get the value of that attribute using the value property (attribute nodes don't have internal text nodes):

 attributes = personNode.attributes                    attendancePerson = attributes.getNamedItem("ATTENDANCE")                   outputText = first_nameNode.firstChild.nodeValue                       + ' ' + last_nameNode.firstChild.nodeValue  + " is " + attendancePerson.value  messageDIV.innerHTML=outputText                   .                   .                   . 

And that's all it takes. Here's what the whole listing looks like:

Listing ch07_05.html
 <HTML>     <HEAD>          <TITLE>              Reading attribute values from XML documents          </TITLE>          <XML ID="meetingsXML" SRC="ch07_01.xml"></XML>          <SCRIPT LANGUAGE="JavaScript">              function readXMLDocument()              {                   var xmldoc, meetingsNode, meetingNode, peopleNode                   var first_nameNode, last_nameNode, outputText                   var attributes, attendancePerson                   xmldoc = document.all("meetingsXML").XMLDocument                   meetingsNode = xmldoc.documentElement                   meetingNode = meetingsNode.firstChild                   peopleNode = meetingNode.lastChild                   personNode = peopleNode.lastChild                   first_nameNode = personNode.firstChild                   last_nameNode = first_nameNode.nextSibling                   attributes = personNode.attributes                   attendancePerson = attributes.getNamedItem("ATTENDANCE")                   outputText = first_nameNode.firstChild.nodeValue                       + ' ' + last_nameNode.firstChild.nodeValue                       + " is " + attendancePerson.value                   messageDIV.innerHTML=outputText              }          </SCRIPT>     </HEAD>     <BODY>         <CENTER>             <H1>                 Reading attribute values from XML documents             </H1>             <INPUT TYPE="BUTTON" VALUE="Get attendance of the third person"                 ONCLICK="readXMLDocument()">             <P>             <DIV ID="messageDIV"></DIV>         </CENTER>     </BODY> </HTML> 

You can see the results in Figure 7-3, where you see that the attendance of the third person is "present."

Figure 7-3. Reading attributes in Internet Explorer.

graphics/07fig03.gif



Real World XML
Real World XML (2nd Edition)
ISBN: 0735712867
EAN: 2147483647
Year: 2005
Pages: 440
Authors: Steve Holzner

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