How to access attributes of nodes in xml.minidom

Mark McEahern marklists at mceahern.com
Thu Apr 11 10:13:09 EDT 2002


[Ruediger Maehl]
> But, how can I access the attributes of a node?

I've modified your example slightly.  I hope it gives you some ideas.  You
can also use getAttribute(name).  I know what you mean about the docs.  But
the xml-sig is a good place to ask these questions.

Cheers,

// mark

#! /usr/bin/env python
import xml.dom.minidom

document = """\
<slideshow>
  <title attrib="new">Demo slideshow</title>
  <slide>
    <title>Slide title</title>
    <point>This is a demo</point>
    <point>Of a program for processing slides</point>
  </slide>
</slideshow>
"""

def main(dom):
    for node in dom.documentElement.childNodes:
        if node.nodeType != 3:
            print node.tagName
        else:
            print node.toxml()
        if node.attributes:
            for i in range(node.attributes.length):
                a = node.attributes.item(i)
                print "%s = %s" % (a.name, a.value)

dom = xml.dom.minidom.parseString(document)

main(dom)

dom.unlink()  # minidom only?
#################






More information about the Python-list mailing list