xml file structure for use with ElementTree?
Andrew Dalke
adalke at mindspring.com
Sat Oct 9 03:55:00 EDT 2004
Stewart Midwinter wrote:
> <?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>
> This script works if I have only one <person> record, but with more
> than one record, it fails with the following error:
...
> 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?
This says your XML is not valid XML. The problem is on
line 3 column 13, which is the "=". You can't do that.
Legal XML would look like
<person name="joe" sex="male" age="49"></person>
or more concisely for empty elements
<person name="joe" sex="male" age="49"/>
> 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'?
from elementtree import ElementTree
tree = ElementTree.parse("tmp.xml").getroot()
for person in tree.findall("person"):
if person.attrib["name"] == "joe":
tree.remove(person)
break
else:
raise AssertionError("Where's joe?")
>>> ElementTree.dump(tree)
<population>
<person age="33" name="hilda" sex="female" />
<person age="17" name="bartholomew" sex="male">
</person>
</population>
>>>
> 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.
The effbot site, and its links to articles by Uche and David,
is the best source for documentation.
Given what you've shown, you need a reference to XML
and not ElementTree. The latter assumes you understand
the former. I don't have one handy.
Andrew
dalke at dalkescientific.com
More information about the Python-list
mailing list