Parse XML using Python
Fredrik Lundh
fredrik at pythonware.com
Thu Dec 9 12:50:29 EST 2004
<anilby at gmail.com> 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()
p.feed("<root>")
p.feed(open("mydocument.xml").read())
p.feed("</root>")
root = p.close()
def printelem(elem, prefix=""):
label = elem.get("label")
if label:
if not prefix:
print
print prefix + label
for elem in elem:
printelem(elem, prefix + "..")
for elem in root:
printelem(elem)
# end
the elementtree library can be found here:
http://effbot.org/zone/element-index.htm
</F>
More information about the Python-list
mailing list