Python XML processing (newbie question)

eichin at eichin at
Tue Feb 11 00:25:25 CET 2003

The way I figured that sort of thing out was to fire up a python
interpreter (in emacs, c-c ! from any python file fires up py-shell)
and then have at it - start with

to see what methods you can run on it, or even
print xmldoc.__doc__
for minimal documentation...

the first thing you should see is childNodes, so you can do
for node in xmldoc.childNodes:
    print node
and then work forward from there, seeing what types you get back from
there.  Other useful bits (from my not yet published half-clone of
perl's XML::Simple) include:
    if node.nodeType == xml.dom.Node.ELEMENT_NODE:
    attr = node.getAttributeNode("id")
    if node.hasAttributes():
    if node.hasChildNodes():
and for coalescing text nodes, bits like
        elif node.nodeType == xml.dom.Node.TEXT_NODE:
            txt = txt + node.nodeValue
        if re.match("^\s*$", str(txt)):
            return None
        return str(txt)

(mostly because xml is unicode, minidom keeps it that way, but in my
limited usage it would be confusing to not have simple strings so
str() downconverts...)

I realize this isn't directly what you're looking for - my usage cared
more about the structure than finding particular deep nodes - but it
might give you some tools to start with...

More information about the Python-list mailing list