2017-01-18 209 views
0

我正在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]

回答

1

您需要使用匹配的文本从elem.find,而不是原来ELEM:

for elem in main_match: 
    subelem = elem.find(searchforxpath) 
    if subelem != None: 
     list.append(subelem.text) 
    else: 
     list.append(False) 
+0

THX对您有所帮助!这正是我需要的:) – Yuli

相关问题