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.par se("population. xml")
root = tree.getroot()
# ...manipulate tree...
tree.write("out file.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\el ementtree\Eleme ntTree.py", line 864, in parse
tree.parse(sour ce, parser)
File "D:\PYTHON23\el ementtree\Eleme ntTree.py", line 588, in parse
parser.feed(dat a)
File "D:\PYTHON23\el ementtree\Eleme ntTree.py", line 1132, in feed
self._parser.Pa rse(data, 0)
xml.parsers.exp at.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
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.par se("population. xml")
root = tree.getroot()
# ...manipulate tree...
tree.write("out file.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\el ementtree\Eleme ntTree.py", line 864, in parse
tree.parse(sour ce, parser)
File "D:\PYTHON23\el ementtree\Eleme ntTree.py", line 588, in parse
parser.feed(dat a)
File "D:\PYTHON23\el ementtree\Eleme ntTree.py", line 1132, in feed
self._parser.Pa rse(data, 0)
xml.parsers.exp at.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
Comment