使用python从子代中查找父代 [英] Finding parent from child in XML using python
问题描述
我对此并不陌生,所以请耐心等待.
I'm new to this, so please be patient.
使用ETree和Python 2.7,我试图解析一个未生成的大型XML文件.基本上,文件包含大量包含的体素组.通用格式为:
Using ETree and Python 2.7, I'm trying to parse a large XML file that I did not generate. Basically, the file contains groups of voxels contained in a large volume. The general format is:
<things>
<parameters>
<various parameters>
</parameters>
<thing id="1" comment="thing1">
<nodes>
<node id="1" x="1" y="1" z="1"/>
<node id="2" x="2" y="2" z="2"/>
</nodes>
<edges>
<edge source="1" target="2"/>
</edges>
</thing>
<thing id="N" comment="thingN">
<nodes>
<node id="3" x="3" y="3" z="3"/>
<node id="4" x="4" y="4" z="4"/>
</nodes>
<edges>
<edge source="3" target="4"/>
</edges>
</thing>
<comments>
<comment node="1" content="interesting feature"/>
<comment node="4" content="interesting feature"/>
</comments>
</things>
节点"包含体素的坐标,物"是一组体素. 注释"用于突出显示感兴趣的节点.
A "node" contains the coordinates of a voxel, and a "thing" is a group of voxels. The "comments" are used to highlight nodes of interest.
我可以使用find命令查找各个节点ID"的属性,例如:
I can find attributes of individual "node ids" using the find command, for example:
for elem in things.iterfind('thing/nodes/node[@id="221"]'):
x = int(elem.get('x'))
我希望能够确定任何节点ID"所属的事物ID"(例如,节点3在事物N中).我知道我可以使用for循环来做到这一点,先遍历事物,然后遍历节点,但是我认为应该有某种方法可以更简单地通过从子代中找到父代来实现.
I'd like to be able to determine the "thing id" to which any "node id" belongs (e.g. node 3 is in thing N). I know that I can do this using a for loop, iterating through the things and then the nodes, but I assume that there should be some way to do it more simply by finding the parent from the child.
我尝试了以下各种变体:
I've tried every variant of:
elem.find(..)
我能想到的
,但我还是得到
that I can think of, but I get either
无类型"或 语法错误(不能在元素上使用绝对路径")
"None Type" or SyntaxError("cannot use absolute path on element")
我也尝试了lxml getparent()命令,这是对以下类似查询的建议:
I've tried the lxml getparent() command, too, as suggested in response to a similar query here: Get parent element after using find method (xml.etree.ElementTree) but to no avail.
是否必须定义此文件中的类才能完全访问XPath工具?
Do I have to define the classes in this file to have complete access to the XPath tools?
推荐答案
您需要向上遍历
for elem in things.iterfind('thing/nodes/node[@id="1"]'):
# get parent of node - nodes
print elem.getparent()
# get grand parent of node - thing
print elem.getparent().getparent()
# now lets get the thing id
print elem.getparent().getparent().attrib.get('id')
这篇关于使用python从子代中查找父代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!