在Python ElementTree中,如何获取树中元素的所有祖先的列表?
我需要“get_ancestors_recursively”函数。
样本运行可以是
>>> dump(tr)
<anc1>
<anc2>
<element> </element>
</anc2>
</anc1>
>>> input_element = tr.getiterator("element")[0]
>>> get_ancestors_recursively(input_element)
['anc1', 'anc2']
有人可以帮我吗?
I need "get_ancestors_recursively" function.
A sample run can be
>>> dump(tr)
<anc1>
<anc2>
<element> </element>
</anc2>
</anc1>
>>> input_element = tr.getiterator("element")[0]
>>> get_ancestors_recursively(input_element)
['anc1', 'anc2']
Can somebody help me with this ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
另一个选项是 LXML,它为内置 ElementTree api 提供了有用的扩展。如果您愿意安装外部模块,它有一个很好的
Element.getparent()
函数,您可以简单地递归调用该函数,直到到达ElementTree.getroot()
。这可能是最快、最优雅的解决方案(因为 lxml.etree 模块为指向其父级的元素引入了指针属性,因此不必在整个树中搜索正确的父级/child 对)。Another option is LXML, which provides useful extensions to the built in ElementTree api. If you're willing to install an external module, it has a nice
Element.getparent()
function that you could simply call recursively until reachingElementTree.getroot()
. This will probably be the fastest and most elegant solution (as thelxml.etree module
introduces pointer attributes for the Elements that point to their parents, so instead of searching the entire tree for the properparent/child
pairs).在最新版本的ElementTree(v1.3或更高版本)中,您可以简单地进行
递归操作。但是,Python 附带的 ElementTree 没有此功能,并且我在 Element 类中没有看到任何向上查找的内容。
我相信这意味着您必须以困难的方式做到这一点:通过对元素树的详尽搜索。
由于 DFS,这很慢,并且会产生大量垃圾收集列表,但如果您可以处理它应该没问题。
In the latest version of ElementTree (v1.3 or later), you can simply do
recursively. However, the ElementTree that ships with Python doesn't have this functionality, and I don't see anything in the Element class that looks upwards.
I believe this means you have to do it the hard way: via an exhaustive search of the element tree.
This is slow because of the DFS, and cranks out a lot of lists for garbage collection, but if you can deal with that it should be fine.
通过大量谷歌搜索找到了这个小宝石(http://elmpowered.skawaii.net/?p=74< /a>)
parent = root.findall(".//{0}/..".format(elem.tag))
root 这里是树的根节点。 elem 是您通过迭代获得的实际元素对象。
这确实需要您了解根源,这可能意味着更改 XML 解析的设置方式,但这充其量是次要的。
Found this little gem from lots of googling (http://elmpowered.skawaii.net/?p=74)
parent = root.findall(".//{0}/..".format(elem.tag))
root here is your root node of the tree. elem is the actual element object you get from iterating.
This does require you to know the root, which may mean changing how you set up for XML parsing, but it's minor at best.
您可以使用树遍历算法。这在线性时间内完成了任务。
You can use a tree traversal algorithm. This does the task in linear time.