使用pythons元素树写入xml文件

时间:2015-11-17 10:49:44

标签: python elementtree

我正在使用python的Element树库来解析/写入xml文件:

<?xml version='1.0' encoding='utf-8'?>
    <data>
      <reminder id="9">
        <enabled>true</enabled>
        <programmename>Show Me Show Me</programmename>
        <starttime>10:05</starttime>
        <sdate>Tuesday, 17 November 2015</sdate>
        <channel>NA</channel>
      </reminder>
    </data>

我正在使用以下内容写出来:

from xml.etree import ElementTree as ET

        data = ET.Element("data")
        reminder = ET.SubElement(data, "reminder")
        reminder.set("id", "9")
        enabled = ET.SubElement(reminder, "enabled")
        enabled.text = "true"
        programmename = ET.SubElement(reminder, "programmename")
        programmename.text = new_programmename
        starttime = ET.SubElement(reminder, "starttime")
        starttime.text = new_programmetime
        sdate = ET.SubElement(reminder, "sdate")
        sdate.text = new_programmedate
        channel = ET.SubElement(reminder, "channel")
        channel.text = "NA"
        indent(data, level=0)
        tree = ET.ElementTree(data)
        tree.write(xmlfile, xml_declaration=True, encoding='utf-8', method="xml")

虽然我很难理解如何添加额外的“记录”/元素?例如,所以xml看起来像这样:

<?xml version='1.0' encoding='utf-8'?>
<data>
<reminder id="9">
    <enabled>true</enabled>
    <programmename>Show Me Show Me</programmename>
    <starttime>10:05</starttime>
    <sdate>Tuesday, 17 November 2015</sdate>
    <channel>NA</channel>
  </reminder>
<reminder id="10">
    <enabled>true</enabled>
    <programmename>foo</programmename>
    <starttime>13:05</starttime>
    <sdate>Wednesday, 18 November 2015</sdate>
    <channel>NA</channel>
  </reminder>
<data>

我将非常感谢任何协助,我无法找到任何参考,或者说我能理解的参考......

1 个答案:

答案 0 :(得分:0)

尝试如下 -

from lxml import etree as et


xml="""<?xml version='1.0' encoding='utf-8'?>
    <data>
      <reminder id="9">
        <enabled>true</enabled>
        <programmename>Show Me Show Me</programmename>
        <starttime>10:05</starttime>
        <sdate>Tuesday, 17 November 2015</sdate>
        <channel>NA</channel>
      </reminder>
    </data>"""

tree = et.fromstring(xml)
element = tree.find(".//reminder[@id='9']")
element_parent  = element.getparent()
element_parent.insert(element_parent.index(element)+1,et.XML("""

<reminder id="10">
    <enabled>true</enabled>
    <programmename>foo</programmename>
    <starttime>13:05</starttime>
    <sdate>Wednesday, 18 November 2015</sdate>
    <channel>NA</channel>
</reminder>"""))

print(et.tostring(tree))

打印 -

<data>
      <reminder id="9">
        <enabled>true</enabled>
        <programmename>Show Me Show Me</programmename>
        <starttime>10:05</starttime>
        <sdate>Tuesday, 17 November 2015</sdate>
        <channel>NA</channel>
      </reminder>
      <reminder id="10">
        <enabled>true</enabled>
        <programmename>foo</programmename>
        <starttime>13:05</starttime>
        <sdate>Wednesday, 18 November 2015</sdate>
        <channel>NA</channel>
      </reminder>
</data>

承认“unutbu”