How do I create new xml from ElementTree?
Bit of a beginner question here:
Say I have a block of xml:
<root>
<district>
<house><room><door/><room></house>
</district>
<district>
<st开发者_JAVA技巧reet>
<house>and so on</house>
</street>
etc.
With ElementTree I can do:
houses=doc.findall(".//house")
to select all the house nodes, regardless of their parent. What I want to do now is turn each of the house nodes into a separate tree object.
Part of the reason for doing this is that I then want to do another find:
door=houseXml.findall(".//door")
I can do something like:
for _house in houses:
houseXml=_house.getiterator
but this doesn't seem to do what I want.
Where am I going wrong?
You can call findall
on the elements returned by the first findall
:
>>> doc = """<root>
... <district>
... <house><room><door/></room></house>
... </district>
... <district>
... <street>
... <house>and so on</house>
... </street>
... </district>
... </root>"""
>>>
>>> from xml.etree import cElementTree as ET
>>>
>>> r = ET.XML(doc)
>>>
>>> for house in r.findall('.//house'):
... print house, house.findall('.//door')
...
<Element 'house' at 0xb7f3ea70> [<Element 'door' at 0xb7f3eab8>]
<Element 'house' at 0xb7f3eb00> []
>>>
精彩评论