Home > Software engineering >  Insert xml element before and after specific elements using python
Insert xml element before and after specific elements using python

Time:03-14

I have to insert two xml element at specific position in a xml file. The predefined xml elements with task tags are given below. That I need to insert at two locations.

<task Alias="" Lnr="2" Object="CC_JOB"> many more elements </task>
<task Alias="" Lnr="x" Object="DC_JOB"> many more elements </task>

CC_JOB need to be inserted after START and DC_JOB need to be inserted before END.

Origin xml file:

<?Xml version='1.0' encoding'utf-8'?>
<uc-export clientvers="12.3.7 build.79r8293r8290">
<JOBP AllowExternal="1" name="JOBP_LOAD_CAT_TXT">
<JOBP state="1">
<JobpStruct mode="design">

<task Alias="" Lnr="1" Object="START">
<many_tags many_attributes="many_values"/>
</task>

<task Alias="" Lnr="2" Object="JOB_ONE">
<many_tags many_attributes="many_values"/>
</task>

<task Alias="" Lnr="3" Object="JOB_TWO">
<many_tags many_attributes="many_values"/>
</task>

<task Alias="" Lnr="4" Object="END">
<many_tags many_attributes="many_values"/>
</task>
</JobpStruct>
</JOBP>
</JOBP>
</uc-export>

My Idea is to search for START object and concat it with element that I need to insert. Similarly for END object append inserting element to END object. I have tried .text, .attrib etc but not luck yet.

import os
form xml.etree import ElementTree as et

def insert_xml_elements(path_of_xml_file):
    for paths, directory, files in os.walk(paths):
        for file in files:
            if file.endswith("xml"):
               filepath = os.path.join(paths, file)
               xmlstring = open(filepath, 'r').read()
               tree = et.fromstring(xmlstring)
               for elem in tree.findall('.//task'):
                   if elem.attrib.get('Object' == 'START':
                        elem.txt = et.tostring(elem, encoding='utf8', method = 'xml')   cc_job_element_string

CodePudding user response:

Try to below (no external lib required)

import xml.etree.ElementTree as ET

task1 = '''<task Alias="" Lnr="2" Object="CC_JOB"> many more elements_1 </task>'''
task2 = '''<task Alias="" Lnr="x" Object="DC_JOB"> many more elements_2 </task>'''

xml = '''<?xml version="1.0" encoding="UTF-8"?>
<uc-export clientvers="12.3.7 build.79r8293r8290">
   <JOBP AllowExternal="1" name="JOBP_LOAD_CAT_TXT">
      <JOBP state="1">
         <JobpStruct mode="design">
            <task Alias="" Lnr="1" Object="START">
               <many_tags many_attributes="many_values" />
            </task>
            <task Alias="" Lnr="2" Object="JOB_ONE">
               <many_tags many_attributes="many_values" />
            </task>
            <task Alias="" Lnr="3" Object="JOB_TWO">
               <many_tags many_attributes="many_values" />
            </task>
            <task Alias="" Lnr="4" Object="END">
               <many_tags many_attributes="many_values" />
            </task>
         </JobpStruct>
      </JOBP>
   </JOBP>
</uc-export>'''

#
# CC_JOB need to be inserted after START and DC_JOB need to be inserted before END.
#
root = ET.fromstring(xml)
task1XML = ET.fromstring(task1)
task2XML = ET.fromstring(task2)
job_struct = root.find('.//JobpStruct')
job_struct.insert(0,task1XML)
job_struct.insert(-2,task2XML)
ET.dump(root)

output

  <uc-export clientvers="12.3.7 build.79r8293r8290">
   <JOBP AllowExternal="1" name="JOBP_LOAD_CAT_TXT">
      <JOBP state="1">
         <JobpStruct mode="design">
            <task Alias="" Lnr="2" Object="CC_JOB"> many more elements_1 </task><task Alias="" Lnr="1" Object="START">
               <many_tags many_attributes="many_values" />
            </task>
            <task Alias="" Lnr="2" Object="JOB_ONE">
               <many_tags many_attributes="many_values" />
            </task>
            <task Alias="" Lnr="x" Object="DC_JOB"> many more elements_2 </task><task Alias="" Lnr="3" Object="JOB_TWO">
               <many_tags many_attributes="many_values" />
            </task>
            <task Alias="" Lnr="4" Object="END">
               <many_tags many_attributes="many_values" />
            </task>
         </JobpStruct>
      </JOBP>
   </JOBP>
</uc-export>
  • Related