简体   繁体   English

问题:for循环内的python xml append元素

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

thanks for taking the time with this one. 感谢您抽出宝贵的时间与这个。 i have an xml file with an element called selectionset. 我有一个带有名为selectionset的元素的xml文件。 the idea is to take that element and modify some of the subelements attributes and tails, that part i have done. 我的想法是采用该元素并修改我完成的部分子元素的属性和尾部。 the shady thing for me to get is why when i try to add the new subelements to the original (called selectionsets) its only pushing the last on the list inplist 我得到的可笑的事情是为什么当我尝试将新的子元素添加到原始的(称为选择集)时,它仅将列表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>

Here's what I've been able to put together and it looks like it'll do what you're looking for. 这是我能够组合在一起的内容,看起来它可以满足您的需求。 Here are the main differences: (1) This will iterate over multiple selectionset items (if you end up with more than one), (2) It creates a deepcopy of the element before modifying the values (I think you were always modifying the original "col"), (3) It appends the new selectionset to the selectionsets tag rather than the root. 主要区别如下:(1)这将遍历多个selectionset项(如果最终导致不止一个),(2)会在修改值之前创建元素的深拷贝(我想您总是在修改原始值) “ col”),(3)它将新的selectionset附加到selectionsets标记而非根目录。

Here's the deepcopy documentation 这是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