Parsing an XML Document to Display Attribute Values

You can get access to an element's attributes with the element's attributes property. You can get attribute names and values with the name and value properties of attribute objects, and I used the value property earlier in this chapter. It's also worth noting that because attributes are themselves nodes, you can use the nodeName and nodeValue properties to do the same thing; I'll do that in this example to show how it works.

Here's how I augment the previous example in our final parsing example in this chapter, looping over all the attributes an element has and listing them (note that you could use the name and value properties here instead of nodeName and nodeValue):

Listing . ch07_08.html ...

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.