Using XML Data Islands

As of version 5 in Internet Explorer, you can also use XML data islands to actually embed XML inside HTML pages. Internet Explorer supports an HTML <XML> element (which is not part of the HTML standard) that you can simply enclose an XML document inside, like this:

 <XML ID="greeting">      <DOCUMENT>         <GREETING>Hi there XML!</GREETING>     </DOCUMENT> </XML> 

The Internet Explorer <XML> element has some attributes worth noting:

  • ID The ID with which you can refer to the <XML> element in code. Set to an alphanumeric string.

  • NS The URI of the XML namespace used by the XML content. Set to a URI.

  • PREFIX Namespace prefix of the XML contents. Set to an alphanumeric string.

  • SRC Source for the XML document, if the document is external. Set to an URI.

When you use this element, you access it using its ID value in code. To reach the element, you can use the all collection, passing it the ID you gave the element like this for the previous example: document.all("greeting") . To get the document object corresponding to the XML document, you can then use the XMLDocument property. Here's how I convert the previous example to use a data island instead of the Microsoft.XMLDOM object:

Listing ch07_03.html
 <HTML>     <HEAD>          <TITLE>              Reading element values with XML data islands          </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  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                   outputText = "Third name: " +                         first_nameNode.firstChild.nodeValue + ' '                       + last_nameNode.firstChild.nodeValue                   messageDIV.innerHTML=outputText              }          </SCRIPT>     </HEAD>     <BODY>         <CENTER>             <H1>                 Reading element values with XML data islands             </H1>             <INPUT TYPE="BUTTON" VALUE="Get the name of the third person"                 ONCLICK="readXMLDocument()">             <P>             <DIV ID="messageDIV"></DIV>         </CENTER>     </BODY> </HTML> 

This example works as the previous example did, as you see in Figure 7-2.

Figure 7-2. Using XML data islands in Internet Explorer.

graphics/07fig02.gif

In the previous example, I used an external XML document, ch07_01.xml, which I referenced with the <XML> element's SRC attribute. However, you can also enclose the entire XML document in the <XML> element, like this:

 <HTML>      <HEAD>          <TITLE>              Creating An XML Data Island          </TITLE>  <XML ID="meetingsXML">   <?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>   </XML>  <SCRIPT LANGUAGE="JavaScript">              function readXMLDocument()              {                   var xmldoc, meetingsNode, meetingNode, peopleNode                   var first_nameNode, last_nameNode, outputText                   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                   outputText = "Third name: " +                         first_nameNode.firstChild.nodeValue + ' '                       + last_nameNode.firstChild.nodeValue                   messageDIV.innerHTML=outputText              }          </SCRIPT>     </HEAD>     <BODY>         <CENTER>             <H1>                 Reading element values with XML data islands             </H1>             <INPUT TYPE="BUTTON" VALUE="Get the name of the third person"                 ONCLICK="readXMLDocument()">             <P>             <DIV ID="messageDIV"></DIV>         </CENTER>     </BODY> </HTML> 

So far, I've used the XMLDocument property of the object corresponding to the XML data island to get the document object. However, you can also use the documentElement property of the data island directly to get the root element of the XML document, like this:

 <HTML>      <HEAD>          <TITLE>              Reading XML element values          </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  meetingsNode = meetingsXML.documentElement  meetingNode = meetingsNode.firstChild                   peopleNode = meetingNode.lastChild                   personNode = peopleNode.lastChild                   first_nameNode = personNode.firstChild                   last_nameNode = first_nameNode.nextSibling                   .                   .                   . </HTML> 


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