3

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

6
  • What happened to the original report? Commented Jun 16, 2016 at 8:02
  • I removed the report attributes as it contained several items and would've cluttered the post, and the attributes wern't really relevent to final goal. I left the report element in there incase there was some reference to it that could help with the positioning of the testcase elements Commented Jun 16, 2016 at 8:08
  • Are you simply asking to append the tags to the testspecif node? Commented Jun 16, 2016 at 8:10
  • Hi, that's the plan, I tried adding the testspecif nodes to my test case files and then merge, but with no luck. This is my first attempt at XML editing, so apologies if it's something glaringly obvious. Commented Jun 16, 2016 at 8:17
  • first an second xml files also have multiple tags yes? Commented Jun 16, 2016 at 8:18

1 Answer 1

5
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>
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the example, just to reiterate, I am hoping to post outside the report node but within the testspecif node (basically on the same hierarchy as the report node), so do I just omit the report line of code and post into test.append instead?
Yep, sorry I don't know why I keep thinking you want to append to the report tag, the edit will add both nodes where you want
Thanks Padraic, your solution looks to work, i'm just having an issue with the tab length on the second test case, (for some reason it looks to be tabbed but with two extra spaces in from the first test case). But with XML, is this not such an issue provided the previous test case closes with </testcase> ? Maybe my xml files need checking for tab structures

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.