Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merging XMLs into parent XML file using Python

I have found a couple of examples of merging XMLs using Python on SO, however I am looking to merge two test case XMLs into a parent XML.

Here's my Parent XML (main.xml)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE propertiesconfiguration_my_campaign>
<exconfig>
  <manager master_node="themaster">
    <testspecif class="MySpec" name="MY_TEST">
      <report>
        *** Report Attributes in here ***
      </report>
        *** Looking to post Test Cases in here ***
    </testspecif>
  </manager>
</exconfig>

I have highlighted in the above XML where I am looking to post the following test case XMLs; these are shown below.

(first.xml)

  <testcase class="CTestCase000001a" name="TC-000001a">
    *** Test case Attributes in here ***
  </testcase>

(second.xml)

  <testcase class="CTestCase000001b" name="TC-000001b">
    *** Test case Attributes in here ***
  </testcase>

Here's the code I am using to merge XMLs, however the code simply dumps the first.xml and second.xml text at the bottom on the main.xml file.

from xml.etree import ElementTree as et

def combine(files):
    first = None
    for filename in files:
        data = et.parse(filename).getroot()
        if first is None:
            first = data
        else:
            first.extend(data)
    if first is not None:
        f = open('C:/temp/newXML.xml', 'wb')
        f.write(et.tostring(first))
        f.close()
        # return et.tostring(first)

combine(('C:/main.xml','C:/first.xml','C:/second.xml'))

What I was hoping to output is something like this, with all the test case XML text embedded in with the report node inside the testspecif element:

<!DOCTYPE propertiesconfiguration_my_campaign>
<exconfig>
  <manager master_node="themaster">
    <testspecif class="MySpec" name="MY_TEST">
      <report>
        *** Report Attributes in here ***
      </report>
      <testcase class="CTestCase000001a" name="TC-000001a">
        *** Test case Attributes in here ***
      </testcase>  
      <testcase class="CTestCase000001b" name="TC-000001b">
        *** Test case Attributes in here ***
      </testcase>       
    </testspecif>
  </manager>
</exconfig>

Any help would be greatly appreciated. Thanks, MikG

like image 843
MikG Avatar asked Dec 06 '25 16:12

MikG


1 Answers

from xml.etree import ElementTree as et


c1, c2 = et.parse("c1.xml"), et.parse("c2.xml")

par_xml = et.parse("par.xml")

test = par_xml.find(".//testspecif")

test.extend([c1.find(".//testcase"), c2.find(".//testcase")])

print(et.tostring(par_xml.getroot()))
# par.write("par.xml",encoding="utf-8", xml_declaration=True)

Which with your example input would give you:

<exconfig>
  <manager master_node="themaster">
    <testspecif class="MySpec" name="MY_TEST">
      <report>

      </report>

    <testcase class="CTestCase000001a" name="TC-000001a">
    *** Test case Attributes in here ***
</testcase>
   <testcase class="CTestCase000001b" name="TC-000001b">
    *** Test case Attributes in here ***
  </testcase>
</testspecif>
  </manager>
</exconfig>
like image 173
Padraic Cunningham Avatar answered Dec 09 '25 15:12

Padraic Cunningham



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!