2016-10-10 39 views
0

ありがとうございました。 私は、selectionsetと呼ばれる要素を持つxmlファイルを持っています。アイデアは、その要素を取って、サブ要素の属性とテールの一部を修正することです。私はその唯一のissue:python xml forループの中に要素を追加してください。

import xml.etree.ElementTree as etree 
from xml.etree.ElementTree import * 
from xml.etree.ElementTree import ElementTree 
tree=ElementTree() 
tree.parse('STRUCTURAL.xml') 
root = tree.getroot() 
col=tree.find('selectionsets/selectionset') 
#find the value needed 
val=tree.findtext('selectionsets/selectionset/findspec/conditions/condition/value/data') 
setname=col.attrib['name'] 
listnames=val + " 6" 
inplist=["D","E","F","G","H"] 
entry=3 
catcher=[] 
ss=root.find('selectionsets') 
outxml=ss 
for i in range(len(inplist)): 
    str(val) 
    col.set('name',(setname +" "+ inplist[i])) 
    col.find('findspec/conditions/condition/value/data').text=str(inplist[i]+val[1:3]) 
    #print (etree.tostring(col)) #everything working well til this point 
    timper=col.find('selectionset') 
    root[0].append(col) 
    # new=etree.SubElement(outxml,timper) 
#you need to create a tree with element tree before creating the xml file 

itree=etree.ElementTree(outxml) 
itree.write('Selection Sets.xml') 
print (etree.tostring(outxml)) 

# print (Test_file.selectionset()) 


#Initial xml 
<?xml version="1.0" encoding="UTF-8" ?> 

<exchange xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://download.autodesk.com/us/navisworks/schemas/nw-exchange-12.0.xsd" units="ft" filename="STRUCTURAL.nwc" filepath="C:\Users\Ricardo\Desktop\Comun\Taller 3"> 
    <selectionsets> 
    <selectionset name="Column Location" guid="565f5345-de06-4f5b-aa0f-1ae751c98ea8"> 
     <findspec mode="all" disjoint="0"> 
     <conditions> 
      <condition test="contains" flags="10"> 
      <category> 
       <name internal="LcRevitData_Element">Element</name> 
      </category> 
      <property> 
       <name internal="lcldrevit_parameter_-1002563">Column Location Mark</name> 
      </property> 
      <value> 
       <data type="wstring">C-A </data> 
      </value> 
      </condition> 
     </conditions> 
     <locator>/</locator> 
     </findspec> 
    </selectionset> 
    </selectionsets> 
</exchange> 

#----Current Output 
<selectionsets> 
    <selectionset guid="565f5345-de06-4f5b-aa0f-1ae751c98ea8" name="Column Location H"> 
     <findspec disjoint="0" mode="all"> 
     <conditions> 
      <condition flags="10" test="contains"> 
      <category> 
       <name internal="LcRevitData_Element">Element</name> 
      </category> 
      <property> 
       <name internal="lcldrevit_parameter_-1002563">Column Location Mark</name> 
      </property> 
      <value> 
       <data type="wstring">H-A</data> 
      </value> 
      </condition> 
     </conditions> 
     <locator>/</locator> 
     </findspec> 
    </selectionset> 
    <selectionset guid="565f5345-de06-4f5b-aa0f-1ae751c98ea8" name="Column Location H"> 
     <findspec disjoint="0" mode="all"> 
     <conditions> 
      <condition flags="10" test="contains"> 
      <category> 
       <name internal="LcRevitData_Element">Element</name> 
      </category> 
      <property> 
       <name internal="lcldrevit_parameter_-1002563">Column Location Mark</name> 
      </property> 
      <value> 
       <data type="wstring">H-A</data> 
      </value> 
      </condition> 
     </conditions> 
     <locator>/</locator> 
     </findspec> 
    </selectionset> 
    <selectionset guid="565f5345-de06-4f5b-aa0f-1ae751c98ea8" name="Column Location H"> 
     <findspec disjoint="0" mode="all"> 
     <conditions> 
      <condition flags="10" test="contains"> 
      <category> 
       <name internal="LcRevitData_Element">Element</name> 
      </category> 
      <property> 
       <name internal="lcldrevit_parameter_-1002563">Column Location Mark</name> 
      </property> 
      <value> 
       <data type="wstring">H-A</data> 
      </value> 
      </condition> 
     </conditions> 
     <locator>/</locator> 
     </findspec> 
    </selectionset> 
    <selectionset guid="565f5345-de06-4f5b-aa0f-1ae751c98ea8" name="Column Location H"> 
     <findspec disjoint="0" mode="all"> 
     <conditions> 
      <condition flags="10" test="contains"> 
      <category> 
       <name internal="LcRevitData_Element">Element</name> 
      </category> 
      <property> 
       <name internal="lcldrevit_parameter_-1002563">Column Location Mark</name> 
      </property> 
      <value> 
       <data type="wstring">H-A</data> 
      </value> 
      </condition> 
     </conditions> 
     <locator>/</locator> 
     </findspec> 
    </selectionset> 
    <selectionset guid="565f5345-de06-4f5b-aa0f-1ae751c98ea8" name="Column Location H"> 
     <findspec disjoint="0" mode="all"> 
     <conditions> 
      <condition flags="10" test="contains"> 
      <category> 
       <name internal="LcRevitData_Element">Element</name> 
      </category> 
      <property> 
       <name internal="lcldrevit_parameter_-1002563">Column Location Mark</name> 
      </property> 
      <value> 
       <data type="wstring">H-A</data> 
      </value> 
      </condition> 
     </conditions> 
     <locator>/</locator> 
     </findspec> 
    </selectionset> 
    <selectionset guid="565f5345-de06-4f5b-aa0f-1ae751c98ea8" name="Column Location H"> 
     <findspec disjoint="0" mode="all"> 
     <conditions> 
      <condition flags="10" test="contains"> 
      <category> 
       <name internal="LcRevitData_Element">Element</name> 
      </category> 
      <property> 
       <name internal="lcldrevit_parameter_-1002563">Column Location Mark</name> 
      </property> 
      <value> 
       <data type="wstring">H-A</data> 
      </value> 
      </condition> 
     </conditions> 
     <locator>/</locator> 
     </findspec> 
    </selectionset> 
    </selectionsets> 
+0

だ、findメソッドは、指定されたパスを持つ最初の要素を検索します。したがって、同じ要素を反復ごとに見つけ出し、変更してルートに追加します。要素を反復して「インプリスト」を増やしたいと思うかもしれません。しかし、これは多くの前提を作り出しています。元のXMLと処理のための完全なコードを見ることができればうれしいでしょう。 –

+0

返事ありがとうございます、それは意味がありますが、私はループ内のプリントを使用している場合、私はいくつかの間違いを犯したことを確かめるためにxml処理に新しい希望の結果を得ます。 – RicardoZepeda

答えて

0

inplistリストの最後を押して、元の(と呼ばれるselectionsets)に新しいサブ要素を追加しようとすると、ここで私は置くことができました何を、なぜ 私が取得するための日陰事があります一緒に、それはあなたが探しているものを行うだろうように見えます。主な違いは次のとおりです:(1)複数の選択セット項目を繰り返します(2つ以上になる場合)。(2)値を変更する前に要素のディープコピーを作成します。 "col")、(3)新しいselectionsetをルートではなくselectionsetsタグに追加します。

は、ここでは、xml.etree.ElementTreeを使用していると仮定すると、deepcopy documentation

import xml.etree.ElementTree as etree 
import copy 

tree=etree.ElementTree() 
tree.parse('test.xml') 
root = tree.getroot() 
inplist=["D","E","F","G","H"] 

for selectionset in tree.findall('selectionsets/selectionset'): 
    for i in inplist: 
     col = copy.deepcopy(selectionset) 
     col.set('name', '%s %s' % (col.attrib['name'], i)) 
     data = col.find('findspec/conditions/condition/value/data') 
     data.text = '%s%s' % (i, data.text[1:3]) 
     root.find('selectionsets').append(col) 

itree = etree.ElementTree(root) 
itree.write('Selection Sets.xml') 
+0

チャンプのような作品!ありがとう、今あなたのコードを勉強しています! – RicardoZepeda

+0

聞いてうれしい! @リカルド・ゼペダ、これがあなたが探していたもので、それがうまくいくなら、それを答えとして受け入れることができますか?このサイトでもう少し手伝うことができます。 –

関連する問題