2017-03-03 74 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