0

Bit of a beginner question here:

Say I have a block of xml:

<root>
 <district>
  <house><room><door/><room></house>
 </district>
 <district>
  <street>
   <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?

1 Answer 1

2

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> []
>>>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.