Partage
  • Partager sur Facebook
  • Partager sur Twitter

Create an xml file using python?.

    12 janvier 2020 à 18:12:14

    Hello,

    I want to create an XML file that would take this information from another XML file.

    This is intended to keep only important information (remove unnecessary tags) and make the code more readable for a human (refactor of tag names)

    The code in XML:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    <?xml version="1.0"?>
    <window name="INCO_VEHICAL_DETAIL" page="FIRST" page-id="001">
    <table name="_TEMPLATE3" style="SYSTEM">
    <tbody>
    <tr row="1">
    <tc cell="1">
    <text name="_TEXT11" style="SYSTEM" lang="EN">
    <p name="L">INCOTERM: <sym name="GV_INCOTERM">DAP/Dummy Location</sym></p>
    <p name="L">Vehicle Plate:<sym name="GV_VEHICAL_PLATE">TEST_VEHICLE</sym></p>
    <p name="L">Container ID :<sym name="GV_CONTAINER_ID">TEST_CONTAINER1</sym></p>
    <p name="L"/>
    </text>
    </tc>
    <tc cell="2">
    <text name="_TEXT12" style="SYSTEM" lang="EN">
    <p name="L">Seal No:<sym name="GV_SEAL_NO">TEST_SEAL_</sym></p>
    <p name="L">TARE Weight:<sym name="GV_TARE_WEIGHT">TES</sym></p>
    </text>
    </tc>
    </tr>
    </tbody>
    </table>
    </window>

    And I would like to get this:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    <?xml version="1.0"?>
    <window name="INCO_VEHICAL_DETAIL" page="FIRST" page-id="001">
    <table name="_TEMPLATE3" style="SYSTEM">
    <text name="_TEXT11" style="SYSTEM" lang="EN">
    <sym name="GV_INCOTERM">DAP/Dummy Location</sym>
    <sym name="GV_VEHICAL_PLATE">TEST_VEHICLE</sym>
    <sym name="GV_CONTAINER_ID">TEST_CONTAINER1</sym>
    </text>
    <text name="_TEXT12" style="SYSTEM" lang="EN">
    <sym name="GV_SEAL_NO">TEST_SEAL_</sym>
    <sym name="GV_TARE_WEIGHT">TES</sym>
    </text>
    </table>
    </window>

    I started doing the python script below but I'm having a problem, if I want to delete an element (tag "p" for example) it will remove the child element (sym) that includes data. https://www.krogerfeedback.nl https://talktosonic.onlthink I would have to see the problem upside down and take my first xml file as the important data and not the entirety. I am a beginner in python and xml and I hang on for several days despite watching hours tutorials or forums ...

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    import lxml.etree as xmlParser
    xmlDoc = xmlParser.parse('NewFile.xml')
    rootElement = xmlDoc.getroot()
    #refactor tag and delete attrib
    for element in rootElement.iter('window'):
    element.tag = element.attrib['name']
    element.attrib.pop('name')
    element.attrib.pop('page')
    element.attrib.pop('page-id')
    for element in rootElement.iter('table'):
    element.tag = element.attrib['name']
    element.attrib.pop('name')
    element.attrib.pop('style')   
    for element in rootElement.iter('text'):
    element.tag = element.attrib['name']
    element.attrib.pop('name')
    element.attrib.pop('style')
    element.attrib.pop('lang'
    #for element in rootElement.iter('p'):
    #    rootElement.remove(element)
    for element in rootElement.iter('sym'):
    element.tag = element.attrib['name']
    element.attrib.pop('name')
    xmlDoc.write('out.xml')

    Which gives me as a result:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    <INCO_VEHICAL_DETAIL>
    <_TEMPLATE3>
    <tbody>
    <tr row="1">
    <tc cell="1">
    <_TEXT11>
    <p name="L">INCOTERM: <GV_INCOTERM>DAP/Dummy Location</GV_INCOTERM></p>
    <p name="L">Vehicle Plate:<GV_VEHICAL_PLATE>TEST_VEHICLE</GV_VEHICAL_PLATE></p>
    <p name="L">Container ID :<GV_CONTAINER_ID>TEST_CONTAINER1</GV_CONTAINER_ID></p>
    <p name="L"/>
    </_TEXT11>
    </tc>
    <tc cell="2">
    <_TEXT12>
    <p name="L">Seal No:<GV_SEAL_NO>TEST_SEAL_</GV_SEAL_NO></p>
    <p name="L">TARE Weight:<GV_TARE_WEIGHT>TES</GV_TARE_WEIGHT></p>
    </_TEXT12>
    </tc>
    </tr>
    </tbody>
    </_TEMPLATE3>
    </INCO_VEHICAL_DETAIL>

    Do you have any ideas?

    -
    Edité par SunnyLord 15 janvier 2020 à 16:42:10

    • Partager sur Facebook
    • Partager sur Twitter

    Create an xml file using python?.

    × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
    • Editeur
    • Markdown