|
I am trying to retrieve data from xml.etree.cElementTree. i have the following code Code Snippet with the above code i am getting the result as below reviews for id=p4645 R3 R5 But i need the output as reviewes for id=p4645 blk - R3 pri - R5 i.e, i need the parent tag along with the element using Xpaths. I know its possible if i iterate through entire tree. But need to use more loops. |
perhaps loop through each stage and check its children
root = ET.fromstring(xmldata)
stages = root.findall("./ep_150/stage")
print '\n\nreviews for id=p4645\n'
for stage in stages:
children = stage.getchildren()
for child in children:
if child.attrib['id']=='p4645':
print('%s - %s' % (stage.attrib['name'], child.attrib['name']))
--
view archives: http://groups.google.com/group/python_inside_maya
change your subscription settings: http://groups.google.com/group/python_inside_maya/subscribe
If you are using the newest cElementTree from pypi then you can also modify your xpath to directly filter the id attribute and save the loop:review = root.findall('./ep_150/stage/review[@id="p4645"]')[" - ".join((parent_map[r].get('name'), r.get('name'))) for r in review]# ['blk - R3', 'pri - R5']