2017-03-03 3 views
0

Ich habe die folgende XML-Input-:Python hinzufügen Tags mit Hilfe von XML lxml

<?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> 

Mein Programm fügt drei Stichworte zum XML, aber sie sind falsch formatiert. Der Output XML sieht wie folgt aus:

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

Das ist meine Quelle-Code:

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") 

Meine Frage ist, wie die neuen Tags im richtigen Format an die XML hinzuzufügen. Ich denke, es funktioniert so, um das geänderte XML erneut zu parsen, aber es ist nicht nett formatiert. Irgendwelche Ideen? Danke im Voraus.

+0

Was ist die gewünschte Ausgabe oder was meinst du mit * richtiges Format *? Beziehen Sie sich nur auf Zeilenumbrüche und Einrückungen? – Parfait

Antwort

0

Um nett recht-gedruckte Ausgabe zu gewährleisten, müssen Sie zwei Dinge tun:

  1. die Eingabedatei Parsen ein XMLParser Objekt mit remove_blank_text=True verwenden.

    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) 
    

    Inhalt output.xml: pretty_print=True

Beispiel

  • schreiben die Ausgabe mit

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

    Siehe auch http://lxml.de/FAQ.html#why-doesn-t-the-pretty-print-option-reformat-my-xml-output.

  • +0

    Danke, es hat sehr gut funktioniert. Ich habe genau nach so etwas gesucht. – Lehtim

    Verwandte Themen