Python使用lxml将标签添加到XML

时间:2017-03-03 11:43:08

标签: python xml 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 个答案:

答案 0 :(得分:0)

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

  1. 使用带有remove_blank_text=True的{​​{3}}对象解析输入文件。
  2. 使用pretty_print=True
  3. 编写输出

    示例:

    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>
    

    另见XMLParser