簡體   English   中英

問題:for循環內的python xml append元素

[英]issue: python xml append element inside a for loop

感謝您抽出寶貴的時間與這個。 我有一個帶有名為selectionset的元素的xml文件。 我的想法是采用該元素並修改我完成的部分子元素的屬性和尾部。 我得到的可笑的事情是為什么當我嘗試將新的子元素添加到原始的(稱為選擇集)時,它僅將列表inplist中的最后一個推入

    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>

這是我能夠組合在一起的內容,看起來它可以滿足您的需求。 主要區別如下:(1)這將遍歷多個selectionset項(如果最終導致不止一個),(2)會在修改值之前創建元素的深拷貝(我想您總是在修改原始值) “ col”),(3)它將新的selectionset附加到selectionsets標記而非根目錄。

這是Deepcopy文檔

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')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM