<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content=text/html;charset=Windows-1252>
<META content="MSHTML 6.00.6000.16788" name=GENERATOR></HEAD>
<BODY id=MailContainerBody
style="PADDING-RIGHT: 10px; PADDING-LEFT: 10px; PADDING-TOP: 15px"
bgColor=#ffffff leftMargin=0 topMargin=0 CanvasTabStop="true"
name="Compose message area">
<DIV><FONT face=Garamond color=#000080>i've got most of this working now so hold
off (for now). thanks.</FONT></DIV>
<DIV><FONT face=Garamond color=#000080></FONT> </DIV>
<DIV><FONT face=Garamond color=#000080>dinesh<BR></DIV></FONT>
<DIV style="FONT: 10pt Tahoma">
<DIV><BR></DIV>
<DIV style="BACKGROUND: #f5f5f5">
<DIV style="font-color: black"><B>From:</B> <A title=dineshbvadhia@hotmail.com
href="mailto:dineshbvadhia@hotmail.com">Dinesh B Vadhia</A> </DIV>
<DIV><B>Sent:</B> Saturday, January 24, 2009 8:31 PM</DIV>
<DIV><B>To:</B> <A title=tutor@python.org
href="mailto:tutor@python.org">tutor@python.org</A> </DIV>
<DIV><B>Subject:</B> Traversing XML Tree with ElementTree</DIV></DIV></DIV>
<DIV><BR></DIV>
<DIV><FONT face=Garamond color=#000080>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:</FONT></DIV>
<DIV><FONT face=Garamond><FONT color=#000080><FONT face=Garamond
color=#000080></FONT></FONT></FONT> </DIV>
<DIV><FONT face=Garamond
color=#000080><a> <BR> <b
att="atttag" content="b"> this is node b
</b> <BR> <c>
this is node
c <BR> <d>
this is node d
</d> <BR> </c> <BR> <e>
this is node e </e> <BR></a> </FONT></DIV>
<DIV><FONT face=Garamond><FONT color=#000080><FONT face=Garamond><FONT
color=#000080><FONT face=Courier></FONT></FONT></FONT></FONT></FONT> </DIV>
<DIV><FONT face=Garamond color=#000080>... then require:</FONT></DIV>
<DIV><FONT face=Garamond><FONT color=#000080><FONT face=Courier
color=#000080></FONT></FONT></FONT> </DIV>
<DIV><FONT face=Garamond color=#000080><a></FONT></DIV>
<DIV><FONT face=Garamond color=#000080><a><b att="atttag"
content="b"> this is node b</FONT></DIV>
<DIV><FONT face=Courier><FONT face=Garamond color=#000080><a><c>
this is node c <BR></FONT></DIV>
<DIV><FONT face=Garamond color=#000080><a><c><d> this is
node d<BR><a><e> this is node e<BR></FONT></DIV>
<DIV><FONT face=Garamond color=#000080>I found this code on the Comp.Lang.Python
list but it doesn't retain the full path.</FONT></DIV>
<DIV><FONT face=Garamond><FONT color=#000080><FONT face=Garamond
color=#000080></FONT></FONT></FONT> </DIV>
<DIV><FONT face=Garamond color=#000080>import xml.etree.ElementTree as
ET<BR>tree = ET.parse(xmlfile)</FONT></DIV>
<DIV><FONT face=Garamond><FONT color=#000080><FONT face=Garamond
color=#000080></FONT></FONT></FONT> </DIV>
<DIV><FONT face=Garamond color=#000080>def traverse(node):</FONT></DIV>
<DIV><FONT face=Garamond color=#000080> for c in
node.getchildren():</FONT></DIV>
<DIV><FONT face=Garamond
color=#000080> print c.tag, ':',
c.text</FONT></DIV>
<DIV><FONT face=Garamond
color=#000080> traverse(c)</FONT></DIV>
<DIV><FONT face=Garamond><FONT color=#000080><FONT face=Garamond><FONT
color=#000080><FONT face=Courier><FONT face=Garamond
color=#000080></FONT></FONT></FONT></FONT></FONT></FONT> </DIV>
<DIV><FONT face=Garamond color=#000080>root =
tree.getroot()<BR>traverse(root)</FONT></DIV>
<DIV><FONT face=Garamond><FONT color=#000080><FONT face=Garamond><FONT
color=#000080><FONT face=Courier><FONT face=Garamond
color=#000080></FONT></FONT></FONT></FONT></FONT></FONT> </DIV>
<DIV><FONT face=Garamond color=#000080>Any ideas how to do this?
Thanks</FONT></DIV>
<DIV><FONT face=Garamond><FONT color=#000080><FONT face=Garamond><FONT
color=#000080><FONT face=Courier><FONT
face=Garamond></FONT></FONT></FONT></FONT></FONT></FONT> </DIV>
<DIV><FONT face=Garamond color=#000080>Dinesh</FONT></DIV>
<DIV><FONT face=Garamond><FONT color=#000080><FONT size=+0><FONT
color=#000080><FONT face=Courier><FONT
face=Garamond></FONT></FONT></FONT></FONT></FONT> </DIV></FONT></FONT></BODY></HTML>