我有一个gxl文件,我想找到它的所有结束节点(叶),并存储每个结束节点的名称(在标签节点,名称属性中)。我意识到在gxl文件中,末端节点是那些有节点标签而没有边标签的节点。
我想找出所有没有边的节点。
那么我该怎么做呢?下面是我的gxl文件示例链接:https://gist.github.com/anonymous/61c1afd751214a0473fd62ee74a3b1d6
例如,这里的节点id270是结束节点,因为它没有任何边标记。:
<node id="N_270">
<attr name="name">
<string>
android.content.Context
java.lang.String getString(int)
</string>
</attr>
</node>
<node id="N_271">
<attr name="name">
<string>android.view.ViewGroup
voidinit(android.content.Context,android.util.AttributeSet,int)
</string>
</attr>
</node>
<edge from="N_271" to="N_291" isdirected="true" id="N_271--N_291">
</edge> 发布于 2017-07-18 01:00:12
考虑使用Python标准库中的xml.etree.ElementTree。
import xml.etree.ElementTree as et
gxl_file_path = "C:\\some\\file\\path\\file.gxl"
tree = et.parse(gxl_file_path)
root = tree.getroot() # At this point you can traverse the node structure as needed假设您需要查找节点的名称:
>>> root.tag
'gxl'或者,如果你想遍历所有的边缘节点:
for edge in root.iter('edge'):
# ... Logic ...我不能确切地说出你想要解析的是什么,但我相信你应该遍历“node”节点,就像这样:
for node in root.iter('node'):
if node.find('attr'): # If the attribute node is present
name = node.find('attr').get('name')https://stackoverflow.com/questions/45149369
复制相似问题