xml file structure for use with ElementTree?

Stewart Midwinter stewart.midwinter at gmail.com
Sat Oct 9 02:30:22 EDT 2004


I want to parse a file with ElementTree.  My file has the following
format:
<!-- file population.xml -->
<?xml version='1.0' encoding='utf-8'?>
<population>
<person><name="joe" sex="male" age="49"></person>
<person><name="hilda" sex="female" age="33"></person>
<person><name="bartholomew" sex="male" age="17">
</person>
</population>
note that the population can have more than one person.

I've created the following script to read and parse this file:
<!-- file et1.py -->
from elementtree import ElementTree
tree = ElementTree.parse("population.xml")
root = tree.getroot()
# ...manipulate tree...
tree.write("outfile.xml")

This script works if I have only one <person> record, but with more
than one record, it fails with the following error:
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "D:\PYTHON23\elementtree\ElementTree.py", line 864, in parse
    tree.parse(source, parser)
  File "D:\PYTHON23\elementtree\ElementTree.py", line 588, in parse
    parser.feed(data)
  File "D:\PYTHON23\elementtree\ElementTree.py", line 1132, in feed
    self._parser.Parse(data, 0)
xml.parsers.expat.ExpatError: not well-formed (invalid token): line 3,
column 13

What am I doing wrong?  Do I need to describe the xml structure in
some way?

2nd question. Assuming I can read and parse this file, I can create
and add an element to the tree.  But how would I go about deleting
from the tree the <person> record for, say, name='joe'?

Is there a source of tutorial information anywhere for the use of
ElementTree? I'm looking for a few more examples than those contained
on the effbot.org site - none of which seem to address the question of
input file structure.

thanks
S



More information about the Python-list mailing list