繁体   English   中英

Python使用lxml将标签添加到XML

[英]Python add Tags to XML using lxml

我有以下输入XML:

<?xml version="1.0" encoding="utf-8"?>
<Scenario xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Scenario.xsd">
  <TestCase>test_startup_0029</TestCase>
  <ShortDescription>Restart of the EVC with missing ODO5 board.</ShortDescription>
  <Events>
    <Event Num="1">Switch on the EVC</Event>
  </Events>
  <HW-configuration>
    <ELBE5A>true</ELBE5A>
    <ELBE5K>false</ELBE5K>
  </HW-configuration>
  <SystemFailure>true</SystemFailure>
</Scenario>

我的程序确实向XML添加了三个标签,但它们的格式为false。 输出XML如下所示:

<Scenario xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Scenario.xsd">
  <TestCase>test_startup_0029</TestCase>
  <ShortDescription>Restart of the EVC with missing ODO5 board.</ShortDescription>
  <Events>
    <Event Num="1">Switch on the EVC</Event>
  </Events>
  <HW-configuration>
    <ELBE5A>true</ELBE5A>
    <ELBE5K>false</ELBE5K>
  </HW-configuration>
  <SystemFailure>true</SystemFailure>
<Duration>12</Duration><EVC-SW-Version>08.02.0001.0027</EVC-SW-Version><STAC-Release>08.02.0001.0027</STAC-Release></Scenario>

那就是我的源代码:

class XmlManager:

    @staticmethod
    def write_xml(xml_path, duration, evc_sw_version):
        xml_path = os.path.abspath(xml_path)
        if os.path.isfile(xml_path) and xml_path.endswith(".xml"):
            # parse XML into etree
            root = etree.parse(xml_path).getroot()
            # add tags
            duration_tag = etree.SubElement(root, "Duration")
            duration_tag.text = duration
            sw_version_tag = etree.SubElement(root, "EVC-SW-Version")
            sw_version_tag.text = evc_sw_version
            stac_release = evc_sw_version
            stac_release_tag = etree.SubElement(root, "STAC-Release")
            stac_release_tag.text = stac_release
            # write changes to the XML-file
            tree = etree.ElementTree(root)
            tree.write(xml_path, pretty_print=False)
        else:
            XmlManager.logger.log("Invalid path to XML-file")


def main():
    xml = r".\Test_Input_Data_Base\blnmerf1_md1czjyc_REL_V_08.01.0001.000x\Test_startup_0029\Test_startup_0029.xml"
    XmlManager.write_xml(xml, "12", "08.02.0001.0027")

我的问题是如何以正确的格式将新标签添加到XML。 我猜想它的工作方式是再次解析更改后的XML,但格式不好。 有任何想法吗? 提前致谢。

为了确保打印出精美的输出,您需要做两件事:

  1. 使用带有remove_blank_text=TrueXMLParser对象解析输入文件。
  2. 使用pretty_print=True写入输出

例:

from lxml import etree

parser = etree.XMLParser(remove_blank_text=True)
tree = etree.parse("Test_startup_0029.xml", parser)
root = tree.getroot()

duration_tag = etree.SubElement(root, "Duration")
duration_tag.text = "12"

sw_version_tag = etree.SubElement(root, "EVC-SW-Version")
sw_version_tag.text = "08.02.0001.0027"

stac_release_tag = etree.SubElement(root, "STAC-Release")
stac_release_tag.text = "08.02.0001.0027"

tree.write("output.xml", pretty_print=True)

output.xml的内容:

<Scenario xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Scenario.xsd">
  <TestCase>test_startup_0029</TestCase>
  <ShortDescription>Restart of the EVC with missing ODO5 board.</ShortDescription>
  <Events>
    <Event Num="1">Switch on the EVC</Event>
  </Events>
  <HW-configuration>
    <ELBE5A>true</ELBE5A>
    <ELBE5K>false</ELBE5K>
  </HW-configuration>
  <SystemFailure>true</SystemFailure>
  <Duration>12</Duration>
  <EVC-SW-Version>08.02.0001.0027</EVC-SW-Version>
  <STAC-Release>08.02.0001.0027</STAC-Release>
</Scenario>

另请参见http://lxml.de/FAQ.html#why-doesn-t-the-pretty-print-option-reformat-my-xml-output

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM