<!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).&nbsp; thanks.</FONT></DIV>
<DIV><FONT face=Garamond color=#000080></FONT>&nbsp;</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.&nbsp; if we 
have:</FONT></DIV>
<DIV><FONT face=Garamond><FONT color=#000080><FONT face=Garamond 
color=#000080></FONT></FONT></FONT>&nbsp;</DIV>
<DIV><FONT face=Garamond 
color=#000080>&lt;a&gt;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;b 
att="atttag" content="b"&gt; this is node b 
&lt;/b&gt;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;c&gt; 
this is node 
c&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;d&gt; 
this is node&nbsp;d 
&lt;/d&gt;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/c&gt;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;e&gt; 
this is node&nbsp;e &lt;/e&gt; <BR>&lt;/a&gt; </FONT></DIV>
<DIV><FONT face=Garamond><FONT color=#000080><FONT face=Garamond><FONT 
color=#000080><FONT face=Courier></FONT></FONT></FONT></FONT></FONT>&nbsp;</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>&nbsp;</DIV>
<DIV><FONT face=Garamond color=#000080>&lt;a&gt;</FONT></DIV>
<DIV><FONT face=Garamond color=#000080>&lt;a&gt;&lt;b att="atttag" 
content="b"&gt; this is node b</FONT></DIV>
<DIV><FONT face=Courier><FONT face=Garamond color=#000080>&lt;a&gt;&lt;c&gt; 
this is node c <BR></FONT></DIV>
<DIV><FONT face=Garamond color=#000080>&lt;a&gt;&lt;c&gt;&lt;d&gt; this is 
node&nbsp;d<BR>&lt;a&gt;&lt;e&gt; 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>&nbsp;</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>&nbsp;</DIV>
<DIV><FONT face=Garamond color=#000080>def traverse(node):</FONT></DIV>
<DIV><FONT face=Garamond color=#000080>&nbsp;&nbsp;&nbsp;&nbsp;for c in 
node.getchildren():</FONT></DIV>
<DIV><FONT face=Garamond 
color=#000080>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print c.tag, ':', 
c.text</FONT></DIV>
<DIV><FONT face=Garamond 
color=#000080>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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>&nbsp;</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>&nbsp;</DIV>
<DIV><FONT face=Garamond color=#000080>Any ideas how to do this?&nbsp; 
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>&nbsp;</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>&nbsp;</DIV></FONT></FONT></BODY></HTML>