问题
Say I have an XML code like this one:
<root>
<a>
<b>
....
</b>
<c>
....
</c>
<d>
....
</d>
</a>
<d><c></c><a></a></d>
</root>
Is there a function to get the grandchildren elements given a certain child node? Foe example, in the XML code above, if I pass 'd', I would like it to return 'c' and 'a'.
I tried getChildren(), but I guess this returns attributes, but not the children elements. I don't even have attributes btw.
Thank you.
回答1:
The root element is iterable:
>>> import xml.etree.ElementTree as ET
>>> xml = "<root><a><b>....</b><c>....</c><d>....</d></a><d><c><a></a></c></d></root>"
>>> root = ET.fromstring(xml)
>>> root
<Element 'root' at 0x7fa86a7ea610>
>>> for child in root:
... print child
...
<Element 'a' at 0x7fa86a7ea650>
<Element 'd' at 0x7fa86a7ea810>
Getting specific grandchild elements:
>>> root = ET.fromstring(xml)
>>> root.find("d").getchildren()
[<Element 'c' at 0x7fce44939650>, <Element 'a' at 0x7fce44939690>]
If you want the tag rather than the ElementTree object:
>>> [e.tag for e in root.find("d").getchildren()]
['c', 'a']
Note that <Element 'c' at 0x7fce44939650>
represents an ElementTree Element
object (same as root
), whose API is defined in the docs
回答2:
Given that root
is the root of your tree:
>>> [grchild for child in root for grchild in child]
[<Element 'b' at 0xb6cbad4c>, <Element 'c' at 0xb6cbaedc>,
<Element 'd' at 0xb6cbad24>, <Element 'c' at 0xb6cbaaa4>]
Ok so let's write a recursive function the Haskell way:
def recurse(node):
for child in node:
print(child)
recurse(child)
>>> node = root.find('d')
>>> recurse(node)
<Element 'c' at 0xb6cbaaa4>
<Element 'a' at 0xb6cbac0c>
回答3:
Assuming you have already, parsed the document, [i.getchildren() for i in root.findall('d')]
is probably what you want!
To be more generic, you can have a function
def getGrandChildOfTag(tagName, root):
return [i.getchildren() for i in root.findall(tagName)]
来源:https://stackoverflow.com/questions/17294093/how-to-get-grandchild-elements-from-elementtree-in-python