In the previous example, the code listed the names of each node in the ch07_01.xml document. However, you can do more than that: You can also use the nodeValue property to list the value of each node, and I'll do that in this section. In addition, you can indicate the type of each node you come across by checking the nodeType property. Here are the possible values for this property:
Here's how I determine the type of a particular node, using a JavaScript switch statement of the kind we saw in the previous chapter: function parseDocument() { documentXML = document.all("meetingsXML").XMLDocument resultsDIV.innerHTML = iterateChildren(documentXML, "") } function iterateChildren(theNode, indentSpacing) { var typeData switch (theNode.nodeType) { case 1: typeData = "element" break case 2: typeData = "attribute" break case 3: typeData = "text" break case 4: typeData = "CDATA section" break case 5: typeData = "entity reference" break case 6: typeData = "entity" break case 7: typeData = "processing instruction" break case 8: typeData = "comment" break case 9: typeData = "document" break case 10: typeData = "document type" break case 11: typeData = "document fragment" break case 12: typeData = "notation" } . . . If the node has a value (which I check by comparing nodeValue to null , which is the value it will have if there is no actual node value), I can display that value like this, as you see in the whole listing: Listing ch07_07.html<HTML> <HEAD> <TITLE> Parsing an XML document and displaying node type and content </TITLE> <XML ID="meetingsXML" SRC="ch07_01.xml"></XML> <SCRIPT LANGUAGE="JavaScript"> function parseDocument() { documentXML = document.all("meetingsXML").XMLDocument resultsDIV.innerHTML = iterateChildren(documentXML, "") } function iterateChildren(theNode, indentSpacing) { var typeData switch (theNode.nodeType) { case 1: typeData = "element" break case 2: typeData = "attribute" break case 3: typeData = "text" break case 4: typeData = "CDATA section" break case 5: typeData = "entity reference" break case 6: typeData = "entity" break case 7: typeData = "processing instruction" break case 8: typeData = "comment" break case 9: typeData = "document" break case 10: typeData = "document type" break case 11: typeData = "document fragment" break case 12: typeData = "notation" } var text if (theNode.nodeValue != null) { text = indentSpacing + theNode.nodeName + " = " + theNode.nodeValue + " (Node type: " + typeData + ")<BR>" } else { text = indentSpacing + theNode.nodeName + " (Node type: " + typeData + ")<BR>" } if (theNode.childNodes.length > 0) { for (var loopIndex = 0; loopIndex < theNode.childNodes.length; loopIndex++) { text += iterateChildren(theNode.childNodes(loopIndex), indentSpacing + " ") } } return text } </SCRIPT> </HEAD> <BODY> <CENTER> <H1> Parsing an XML document and displaying node type and content </H1> </CENTER> <CENTER> <INPUT TYPE="BUTTON" VALUE="Parse and display the document" ONCLICK="parseDocument()"> </CENTER> <DIV ID="resultsDIV"></DIV> </BODY> </HTML> And that's all it takes. Now you can see the results in Figure 7-5. As you see there, the entire document is listed, as is the type of each node. In addition, if the node has a value, that value is displayed. Figure 7-5. Using JavaScript to display element content and type.
This example listed the nodes of a document. On the other hand, some of the elements in ch07_01.xml have attributes as well. So how do you handle attributes? |