iterate over a series of nodes in an XML file
rajarshi.guha at gmail.com
rajarshi.guha at gmail.com
Wed Jul 5 15:57:32 EDT 2006
Stefan Behnel wrote:
> rajarshi.guha at gmail.com wrote:
> > I have an XML file which contains entries of the form:
> >
> > <idlist>
> > <myID>1</myID>
> > <myID>2</myID>
> > ....
> > <myID>10000</myID>
> > </idlist>
Thanks to everybody for the pointers. ElementTree is what I ended up
using and my looks like this (based on the ElementTree tutorial code):
def extractIds(filename):
f = open(filename,'r')
context = ET.iterparse(f, events=('start','end'))
context = iter(context)
even, root = context.next()
for event, elem in context:
if event == 'end' and elem.tag == 'Id':
yield elem.text
root.clear()
As a result I can do:
for id in extractIds(someFileName):
do something
More information about the Python-list
mailing list