Parse XML using Python

Fredrik Lundh fredrik at
Thu Dec 9 18:50:29 CET 2004

<anilby at> wrote:

> <abcd label="ABC">

> </abcd>
> .
> .
> <xyz label="A1">

> </xyz>
> ..
> and so on

an XML document can only have a single root element, but your example
has at least two top-level elements (abcd and xyz).

here is some elementtree code that handles this by wrapping your data in
a "root" element.

from elementtree import ElementTree

p = ElementTree.XMLTreeBuilder()


root = p.close()

def printelem(elem, prefix=""):
    label = elem.get("label")
    if label:
        if not prefix:
        print prefix + label
    for elem in elem:
        printelem(elem, prefix + "..")

for elem in root:

# end

the elementtree library can be found here:


More information about the Python-list mailing list