for the xml
<grandparent>
<parent1>
<child>data1</child>
</parent1>
<parent2>
<child>data2</child>
</parent2>
</grandparent>
I need the list containing tuples of parent,data for each parent in xml.
Is there a way to do it USING cElementTree? I am able to do it for child,data, but unfortunately child is identical in all the values, hence it is of not much use.
Find node's parent using the path: parent_node = node. find('..')
The parent of each node is found by accessing the parentNode property which returns the parent node if any. A while loop is used until the parent required is found or no more parent elements exist. Inside this loop, each element's parent node is found in every iteration.
It seems you can get access to the parent from the child using version 1.3 of ElementTree (check http://effbot.org/zone/element-xpath.htm), by using xpath commands like child.find('../parent')
. But I think python ships with version 1.2 or something.
You should also check for lxml which is compatible with etree and has full Xpath support http://lxml.de/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With