2017-01-18 2 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

関連する問題