Python ElementTree:在循环中替换元素

时间:2018-12-05 02:27:24

标签: python xml attributes elementtree

我正在尝试创建一个脚本,该脚本循环创建xml文件,并为两个元素增加值。 (使用netaddr的IP地址,以及递增的tag / member元素,tag01-tag10)

from netaddr import IPNetwork
import xml.dom.minidom
import lxml.etree as etree
import xml.etree.cElementTree as ET

ip = IPNetwork('10.10.10.0/24')
count = 1
tag = range(1,10)

uid = ET.Element("message")
type = ET.SubElement(uid, "type").text = "update"
payload = ET.SubElement(uid, "payload")
register = ET.SubElement(payload, "register")
entry = ET.SubElement(register, "entry", ip="11.11.11.11")
tag = ET.SubElement(entry, "tag")
ET.SubElement(tag, "member").text = "tag1"
tree = ET.ElementTree(uid)

while count <= 10:
    elemtag = tree.findall(".//member")
    for elemt in elemtag:
        elemt.text = 'tag{}'.format(tag)
    elemip = tree.findall(".//entry")
    for elemi in elemip:
        elemi.text = 'ip="{}"'.format(ip)
    count += 1
    ET.dump(uid)
    print(count)

    #tree.write("tmp.xml")
    #x = etree.parse("tmp.xml")
    #print etree.tostring(x, pretty_print=True)
    #etree.parse("tmp.xml").write("pretty.xml", encoding="utf-8", pretty_print=True)
    #os.system('tool.py -f pretty.xml')

我弄清楚了如何使用ElementTree创建所需的xml,如果我注释掉了循环并编写了生成的xml,它是正确的,看起来不错,并且可以使用消耗它的工具(是的!)。

<message>
  <type>update</type>
  <payload>
    <register>
      <entry ip="11.11.11.11">
        <tag>
          <member>tag1</member>
        </tag>
      </entry>
    </register>
  </payload>
</message>

但是,当我添加循环以替换两个元素的值时,我似乎无法正确理解,并且正在破坏树中的标签/元素。

<message>
  <type>update</type>
  <payload>
    <register>
      <entry ip="11.11.11.11">ip="10.10.10.0/24"<tag><member>tag&lt;Element 'tag' at 
0x7f7b29d66c90&gt;</member></tag></entry>
    </register>
  </payload>
</message>

我一直在尝试不同的方法来替换元素,但是它们最终都以错误的不同排列而告终,而且我似乎无法正确地做到这一点。希望有人可以帮助我弄清楚我所缺少的。预先感谢!

1 个答案:

答案 0 :(得分:0)

是元素,“ ip”是该元素的属性。
我们可以通过以下方式更改元素的属性:

tree.find('.//entry').attrib['ip'] = "22.22.22.22"

“ tag1”是元素内的.text,但我们在这一行重新分配了tag变量:

tag = ET.SubElement(entry, "tag")

根据我们将自己的标签对象分配给标签元素的文本,而不是标签编号。
我更新了您的代码。现在,此解决方案在每次“计数”迭代时生成新的XML 。希望对您有所帮助。

from netaddr import IPNetwork
import xml.etree.cElementTree as ET

ip = IPNetwork('10.10.10.0/24')
tag_lst = list(range(1, 11))
count = 1

uid = ET.Element("message")
type = ET.SubElement(uid, "type").text = "update"
payload = ET.SubElement(uid, "payload")
register = ET.SubElement(payload, "register")
entry = ET.SubElement(register, "entry", ip="11.11.11.11")
tag = ET.SubElement(entry, "tag")
ET.SubElement(tag, "member").text = "tag1"
tree = ET.ElementTree(uid)

while count <= 10:
    tree.find('.//member').text = "tag"+str(count)
    tree.find('.//entry').attrib['ip'] = format(ip[count])
    count += 1
    ET.dump(uid)
    print(count)

XML输出为:

<message><type>update</type><payload><register><entry ip="10.10.10.1"><tag><member>tag1</member></tag></entry></register></payload></message>
<message><type>update</type><payload><register><entry ip="10.10.10.2"><tag><member>tag2</member></tag></entry></register></payload></message>
<message><type>update</type><payload><register><entry ip="10.10.10.3"><tag><member>tag3</member></tag></entry></register></payload></message>
<message><type>update</type><payload><register><entry ip="10.10.10.4"><tag><member>tag4</member></tag></entry></register></payload></message>
<message><type>update</type><payload><register><entry ip="10.10.10.5"><tag><member>tag5</member></tag></entry></register></payload></message>
<message><type>update</type><payload><register><entry ip="10.10.10.6"><tag><member>tag6</member></tag></entry></register></payload></message>
<message><type>update</type><payload><register><entry ip="10.10.10.7"><tag><member>tag7</member></tag></entry></register></payload></message>
<message><type>update</type><payload><register><entry ip="10.10.10.8"><tag><member>tag8</member></tag></entry></register></payload></message>
<message><type>update</type><payload><register><entry ip="10.10.10.9"><tag><member>tag9</member></tag></entry></register></payload></message>
<message><type>update</type><payload><register><entry ip="10.10.10.10"><tag><member>tag10</member></tag></entry></register></payload></message>