2017-07-17 53 views
0

我有一個gxl文件,我想要找到它的所有末端節點(葉)並存儲每個末端節點名稱(在標籤節點中,名稱屬性)。 我意識到,在gxl文件中,末端節點是具有節點標籤並且沒有邊緣標籤的節點。Python:在gxl文件中找到末端節點(葉)

我想找到所有沒有任何邊緣的節點。

那麼我該怎麼做呢? 這裏是我的GXL文件樣本鏈接: https://gist.github.com/anonymous/61c1afd751214a0473fd62ee74a3b1d6

例如這裏節點ID 270是末端節點,因爲它沒有任何邊緣的標籤。 :

<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> 
+0

請在您的問題的正文中提供一個小測試用例。 https://stackoverflow.com/help/mcve – jdv

回答

0

考慮使用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 ... 

我不能告訴什麼你試圖解析,但我相信你應該迭代'節點'節點,如下所示:

for node in root.iter('node'): 
    if node.find('attr'): # If the attribute node is present 
     name = node.find('attr').get('name') 
+0

感謝您的回答。 我如何找到最終節點?例如這裏節點ID 270是末端節點,因爲它沒有任何邊緣標籤。 : <節點ID = 「N_270」> android.content.Context java.lang.String中的getString(INT) <節點ID = 「N_271」> android.view.ViewGroup空隙的init(android.content.Context,android.util.AttributeSet,INT) <來自= 「N_271」 來=邊緣」 N_291「isdirected =」true「id =」N_271 - N_291「> –

+0

這不是您提供的.gxl文件的有效摘錄 - 我無法理解您希望如何指示'結束'...但是,我已經爲您提供了遍歷xml文件的文檔以及一些示例,這樣你可以使用這些信息並自己找出來。 – flevinkelming

+0

我想找到所有沒有任何邊緣的節點。代碼是什麼? –