I am horrified that I can do the below using VBA and not python. I am trying to parse returned xml from an api into a usable format. Based on the below sample of the structure this needs to perform nested looping. The trouble is that the outermost loop is returning a detached Element from the tree so findall or iterfind get nothing and the nested loops abort. I tried this using 3.4.1 and 2.7.8 and get the same results. This makes no sense to me.
import xml.etree.ElementTree as ET
data = """
<root>
<c1>
<c2>C2 Value 1</c2>
<s1>
<s2> S2 Value 1</s2>
<p1>
<p2>P2 Value 1</p2>
</p1>
<p1>
<p2>P2 Value 2</p2>
</p1>
</s1>
<s1>
<s2> S2 Value 2</s2>
<p1>
<p2>P2 Value 3</p2>
</p1>
</s1>
</c1>
</root>
"""
def use_et():
doc = ET.fromstring(data)
result = ['','','']
for c in doc.findall('.//c2'):
result[0] = c.text
# nothing here executes
# c is a detached Element. list(c) = []
for s in c.findall('..//s2'):
result[1] = s.text
for p in s.iterfind('..//p2'):
result[2] = p.text
print(','.join(result))
use_et()