[Tutor] Fw: Traversing XML Tree with ElementTree
Dinesh B Vadhia
dineshbvadhia at hotmail.com
Sun Jan 25 08:00:00 CET 2009
i've got most of this working now so hold off (for now). thanks.
dinesh
From: Dinesh B Vadhia
Sent: Saturday, January 24, 2009 8:31 PM
To: tutor at python.org
Subject: Traversing XML Tree with ElementTree
I want to traverse an xml file and at each node retain the full path from the parent to that node ie. if we have:
<a>
<b att="atttag" content="b"> this is node b </b>
<c> this is node c
<d> this is node d </d>
</c>
<e> this is node e </e>
</a>
... then require:
<a>
<a><b att="atttag" content="b"> this is node b
<a><c> this is node c
<a><c><d> this is node d
<a><e> this is node e
I found this code on the Comp.Lang.Python list but it doesn't retain the full path.
import xml.etree.ElementTree as ET
tree = ET.parse(xmlfile)
def traverse(node):
for c in node.getchildren():
print c.tag, ':', c.text
traverse(c)
root = tree.getroot()
traverse(root)
Any ideas how to do this? Thanks
Dinesh
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090124/7cb90795/attachment.htm>
More information about the Tutor
mailing list