[Tutor] how to extract text by specifying an element using ElementTree

Kent Johnson kent37 at tds.net
Thu Dec 8 21:45:43 CET 2005


ps python wrote:
>  Kent and Dany, 
> Thanks for your replies.  
> 
> Here fromstring() assuming that the input is in a kind
> of text format. 

Right, that is for the sake of a simple example.
> 
> what should be the case when I am reading files
> directly. 
> 
> I am using the following :
> 
> from elementtree.ElementTree import ElementTree
> mydata = ElementTree(file='00001.xml')
> iter = root.getiterator()
> 
> Here the whole XML document is loaded as element tree
> and how should this iter into a format where I can
> apply findall() method. 

Call findall() directly on mydata, e.g.
for process in mydata.findall('//biological_process'):
   print process.text

The path //biological_process means find any biological_process element 
at any depth from the root element.

Kent



More information about the Tutor mailing list