Parsing an XML Document to Display Node Type and Content

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:

  • 1: Element

  • 2: Attribute

  • 3: Text

  • 4: CDATA section

  • 5: Entity reference

  • 6: Entity

  • 7: Processing instruction

  • 8: Comment

  • 9: Document

  • 10: Document type

  • 11: Document fragment

  • 12: Notation

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 ...

Get Real World XML now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.