I would like to parse nested elements. I do not mind using XPath or Element. For example, a few of the values I would like to print are at:
>>> root[0][0][0][0][0].tag
'{http://www.domain.com/somepath/Schema}element'
>>> root[0][0][0][0][0].text
'findme'
What would be the ideal method to iterate through the XML document, parse, and print the element
values? Here is an example of the schema I am working with.
<?xml version="1.0" encoding="UTF-8"?>
<data xsi:schemaLocation="http://www.domain.com/somepath/Schema file.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.domain.com/somepath/Schema">
<one stuff0="" stuff1="">
<two stuff0="" stuff1="">
<three>
<four stuff0="234234" stuff1="234324">
<element>findme</element>
</four>
<four stuff0="234234" stuff1="234324">
<element>findme2</element>
</four>
<four stuff0="234234" stuff1="234324">
<element>findme3</element>
</four>
</three>
</two>
</one>
<one stuff0="" stuff1="">
<two stuff0="" stuff1="">
<three>
<four stuff0="234234" stuff1="234324">
<element>findme4</element>
</four>
<four stuff0="234234" stuff1="234324">
<element>findme5</element>
</four>
<four stuff0="234234" stuff1="234324">
<element>findme6</element>
</four>
</three>
</two>
</one>
</data>
I have tried the following though no results are returned. Even if this did work it would not see though elements under root1[0]1[0][0] and so on:
>>> for tagname in root[0][0][1][0][0].findall('element'):
... name = tree.get('element')
... print name
...
>>>
Per this question, I have also tried the following without success:
>>> for elem in doc.findall('one/two/three/four'):
... print value.get('stuff1'), elem.text
...
>>>
Problem found:
The element was not being read due to lack of namespace specification which I learned after reading Need Help using XPath in ElementTree. So the following example works:
>>> import xml.etree.cElementTree as ET
>>> for event, element in ET.iterparse("schema.xml"):
... if element.tag == "{http://www.domain.com/somepath/Schema}element":
... print element.text
...
findme
findme2
findme3
findme4
findme5
findme6