2017-03-03 91 views
0

添加标签,以XML,我有以下输入XML:使用Python lxml的

​​

我的计划做了三个标签添加到XML,但他们的格式错误。 输出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,但其格式不是很好。有任何想法吗?提前致谢。

+0

什么是期望的输出或你的意思*正确的格式*?你只是指换行符和缩进? – Parfait

回答

0

为了保证好的漂亮的打印输出,你需要做两件事情:

  1. 使用XMLParser对象解析输入文件,remove_blank_text=True
  2. 收件使用pretty_print=True

实施例的输出:与Output.xml的

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) 

内容:

<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见。

+0

感谢它非常好。我已经精确地寻找那样的东西。 – Lehtim