首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python :在gxl文件中查找结束节点(叶)

问Python :在gxl文件中查找结束节点(叶)
EN

Stack Overflow用户
提问于 2017-07-18 00:34:26
回答 1查看 131关注 0票数 0

我有一个gxl文件,我想找到它的所有结束节点(叶),并存储每个结束节点的名称(在标签节点,名称属性中)。我意识到在gxl文件中,末端节点是那些有节点标签而没有边标签的节点。

我想找出所有没有边的节点。

那么我该怎么做呢?下面是我的gxl文件示例链接:https://gist.github.com/anonymous/61c1afd751214a0473fd62ee74a3b1d6

例如,这里的节点id270是结束节点,因为它没有任何边标记。:

代码语言:javascript
复制
<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> 
EN

回答 1

Stack Overflow用户

发布于 2017-07-18 01:00:12

考虑使用Python标准库中的xml.etree.ElementTree。

代码语言:javascript
复制
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

假设您需要查找节点的名称:

代码语言:javascript
复制
>>> root.tag
'gxl'

或者,如果你想遍历所有的边缘节点:

代码语言:javascript
复制
for edge in root.iter('edge'):
    # ... Logic ...

我不能确切地说出你想要解析的是什么,但我相信你应该遍历“node”节点,就像这样:

代码语言:javascript
复制
for node in root.iter('node'):
    if node.find('attr'):  # If the attribute node is present
        name = node.find('attr').get('name')
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45149369

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档