[英]Check if node exists
我正在Dynamo中使用IronPython 2.7。 我需要檢查節點是否存在。 如果是這樣,則應將節點中的文本寫入列表。 如果否,則應將False寫入列表。
我沒有錯。 但是,即使列表中存在節點,它也不會在列表中寫入文本。 False被正確地寫入列表。
簡單的例子:
<note>
<note2>
<yolo>
<to>
<type>
<game>
<name>Jani</name>
<lvl>111111</lvl>
<fun>2222222</fun>
</game>
</type>
</to>
<mo>
<type>
<game>
<name>Bani</name>
<fun>44444444</fun>
</game>
</type>
</mo>
</yolo>
</note2>
</note>
因此,節點lvl
僅在第一節點game
。 我希望結果列表像list[11111, false]
。
這是我的代碼:
import clr
import sys
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *
sys.path.append("C:\Program Files (x86)\IronPython 2.7\Lib")
import xml.etree.ElementTree as ET
xml="note.xml"
main_xpath=".//game"
searchforxpath =".//lvl"
list=[]
tree = ET.parse(xml)
root = tree.getroot()
main_match = root.findall(main_xpath)
for elem in main_match:
if elem.find(searchforxpath) is not None:
list.append(elem.text)
else:
list.append(False)
print list
為什么列表中的字符串應該為空? 我得到list[ ,false]
。
您需要使用來自elem.find而不是原始elem的匹配文本:
for elem in main_match:
subelem = elem.find(searchforxpath)
if subelem != None:
list.append(subelem.text)
else:
list.append(False)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.