2016-10-10 105 views
0

感謝您抽出時間與這個。 我有一個名爲selectionset的元素的XML文件。這個想法是採取這個元素,並修改一些子元素的屬性和尾巴,這一部分我已經完成了。 ,我讓背陰的事情就是爲什麼當我嘗試新的子元素添加到原來的(稱爲selectionsets)其唯一推過去就行了inplist問題:python xml append 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

假設你正在使用xml.etree.ElementTree,查找方法只查找在指定的路徑的第一個元素。因此,每次迭代都會找到相同的元素,然後修改它,然後將其附加到根。您可能需要迭代元素並增加「inplist」。但是,這是一個假設。能夠看到原始的XML和完整的代碼進行處理會很好。 –

+0

感謝您的回覆,這是有道理的,但如果我使用循環內的打印它似乎是工作我得到了渴望的結果我新的XML處理,所以即時我確定我犯了一些錯誤。 – RicardoZepeda

回答

0

這裏是我已經能夠把一起,看起來它會做你想要的。以下是主要區別:(1)這將迭代多個選擇集項目(如果最終得到多個選項集),(2)它在修改值之前創建元素的深度拷貝(我認爲您總是修改原始「col」),(3)它將新選擇集附加到選擇集標記而不是根。

這裏的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

很高興聽到! @RicardoZepeda,如果這是你正在尋找和它的作品,你能接受它作爲一個答案,以便我可以建立我的代表了一下?它可以讓我在這個網站上多一點幫助。 –