XML.XMLDocument (Property)

A reference to the top of a DOM hierarchy that describes the content of the XML data island.

Availability:

JScript - 5.0
Internet Explorer - 5.0
JavaScript syntax:IEmyXML.XMLDocument

Given the example of a block of XML in an HTML document:

   <XML ID="myBlock">

   <METADATA>

   <OWNER>Wrox</OWNER>

   <DATATYPE>Example</DATATYPE>

   <ABSTRACT>This is an example block of text.</ABSTRACT>

   </METADATA>

   </XML>

individual nodes in that so called data island can be accessed through this XMLDocument property. The object returned by this property responds to the selectSingleNode() method. The argument to this is the slash separated path to the node within the document you are looking for. The slash separated values are the XML tagnames used to construct the document.

In this example, they all begin with the string "METADATA" and since the document only contains one layer inside that, all nodes can be reached with the following strings:

Given that our XML block has an ID value of "myBlock" this line of script code should yield a reference to an object that encapsulates the <ABSTRACT> node:

myBlock.XMLDocument.selectSingleNode("METADATA/ABSTRACT")

Having accessed the DOM node you want, its content can be examined by looking at its text property.

The example code illustrates this concept as it might be assembled together in a simple form.

Warnings:

Example code:

   <HTML>

   <HEAD>

   </HEAD>

   <BODY>

   <!-- Create an XML island -->

   <XML ID="myBlock">

   <METADATA>

   <OWNER TYPE="PUBLISHER">Wrox</OWNER>

   <DATATYPE>Example</DATATYPE>

   <ABSTRACT>This is an example block of text.</ABSTRACT>

   </METADATA>

   </XML>

   <SCRIPT>

   // Get the DOM document

   myXMLDocument = myBlock.XMLDocument;

   // Find the node

   myNode = myXMLDocument.selectSingleNode("METADATA/ABSTRACT");

   // Display the text in the node

   alert(myNode.text);

   // Now access node attributes and content

   myOwner = myXMLDocument.getElementsByTagName('OWNER')[0];

   alert(myOwner.getAttribute('TYPE') + ': ' + myOwner.text);

   </SCRIPT>

   </BODY>

   </HTML>