only for RuBoard |
One of the most common programming tasks you must perform when working with the DOM document is navigating through the nodes in the document. In this section, you look at a simple example that recursively traverses through all the nodes in the document and displays the name , value , and type properties of the nodes. This example introduces you to the IXMLDOMNamedNodeMap and the IXMLDOMNodeList interfaces and gives you a feel of the flattened view (the "everything-is-a-node" approach), as discussed earlier in the "DOM and the MSXML Interfaces" section. You use an XML document that's similar to Listing 5.1. It is repeated here with an inline DTD and the
comments removed (the file is named displayNodes.xml ):
<?xml version="1.0" encoding="utf-8" ?> <!DOCTYPE Customers [ <!ELEMENT Customers (Customer)* > <!ELEMENT Customer (CompanyName,Contact) > <!ATTLIST Customer id CDATA #REQUIRED > <!ELEMENT CompanyName (#PCDATA) > <!ELEMENT Contact (FirstName , LastName , Title ) > <!ELEMENT FirstName (#PCDATA) > <!ELEMENT LastName (#PCDATA) > <!ELEMENT Title (#PCDATA) > ]> <Customers> <Customer id="ALFKI"> <CompanyName>Alfreds Futterkiste</CompanyName> <Contact> <FirstName>Maria</FirstName> <LastName>Anders</LastName> <Title>Sales Representative</Title> </Contact> </Customer> <Customer id="THEBI"> <CompanyName>The Big Cheese</CompanyName> <Contact> <FirstName>Liz</FirstName> <LastName>Nixon</LastName> <Title>Marketing Manager</Title> </Contact> </Customer> </Customers>
Listing 5.5 shows the DisplayNode.htm file.
<html> <head> <title>Nodes Display</title> </head> <body> <div id="customers"> </div> </body> </html> <script language="javascript"> var NODE_ELEMENT = 1; var NODE_ATTRIBUTE = 2; var NODE_TEXT = 3; var NODE_CDATA = 4; var NODE_ENTITY_REFERENCE = 5; var NODE_ENTITY = 6; var NODE_PROCESSING_INSTRUCTION = 7; var NODE_COMMENT = 8; var NODE_DOCUMENT = 9; var NODE_DOCUMENT_TYPE = 10; var NODE_DOCUMENT_FRAGMENT = 11; var NODE_NOTATION = 12; var objXML = new ActiveXObject("MSXML2.DOMDocument.4.0"); objXML.async = false; objXML.load("displayNodes.xml"); var objDocElement = objXML.documentElement; var indent1 = " "; var indent2 = indent1 + indent1; displayNode( objXML ,"" ); function displayNode(node , strSpaces) { customers.innerHTML += strSpaces + indent1 + "<B><I>Name</I></B>: " + node.nodeName + " <B><I>Value</I></B>: " + node.nodeValue + " <B><I>Type</I></B>: " + node.nodeTypeString + "<BR>" if( node.nodeType == NODE_ELEMENT ) { var namedNodeMap = node.attributes; for (var attr = namedNodeMap.nextNode(); attr != null; attr = namedNodeMap.nextNode()) { displayNode(attr, strSpaces + indent2); } } if(node.nodeType == NODE_DOCUMENT node.nodeType == NODE_ELEMENT) { var nodeList = node.childNodes; for(var childNode = nodeList.nextNode(); childNode != null; childNode = nodeList.nextNode()) { displayNode(childNode , strSpaces + indent1); } } } </script>
You pass the DOM document's object to the displayNode method. The first statement in the method displays the node properties. In the case of an element node, you get the NamedNodeMap collection that enables you to navigate through the attributes in the element. For each occurrence of an attribute, this method calls itself recursively to display the properties of the attribute node. But, in the case of an attribute node, the method exits after the node properties are displayed. In the case of the document object or an element node, you navigate through the NodeList collection to find the child nodes of the current node and call the displayNode method recursively until you reach the leaf nodes under each node.
The nodeType values used in this example are defined by the IXMLDOMNodeType enumerations that specify valid settings for the DOM node type. Table 5.5 lists these enumerations.
IXMLDOMNodeType Enumerations | IXMLDOMNode nodeType | IXMLDOMNode nodeTypeString |
---|---|---|
NODE_ELEMENT | 1 | element |
NODE_ATTRIBUTE | 2 | attribute |
NODE_TEXT | 3 | Text |
NODE_CDATA_SECTION | 4 | cdatasection |
NODE_ENTITY_REFERENCE | 5 | entityreference |
NODE_ENTITY | 6 | entity |
NODE_PROCESSING_INSTRUCTION | 7 | processinginstruction |
NODE_COMMENT | 8 | Comment |
NODE_DOCUMENT | 9 | Document |
NODE_DOCUMENT_TYPE | 10 | documenttype |
NODE_DOCUMENT_FRAGMENT | 11 | documentfragment |
NODE_NOTATION | 12 | Notation |
Figure 5.4 shows the display from the call to displayNode with proper indentation using the   ; entity reference in HTML.
only for RuBoard |