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