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:
METADATA/OWNER
METADATA/DATATYPE
METADATA/ABSTRACT
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.
This property returns an undefined value in the Macintosh version of MSIE 5.0 instead of a reference to a DOM document.
<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>
Prev | Home | Next |
XML.type | Up | XMP object |
JavaScript Programmer's Reference, Cliff Wootton Wrox Press (www.wrox.com) Join the Wrox JavaScript forum at p2p.wrox.com Please report problems to support@wrox.com © 2001 Wrox Press. All Rights Reserved. Terms and conditions. |