我已经在stackoverflow上找到了几个使用Python合并XML的示例,但是我想将两个测试用例XML合并成一个父级XML。
这是我的父级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>
我在上面的XML中标记了我要发布以下测试用例XML的位置; 它们如下所示。
(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>
这是我用来合并XML文件的代码,但是该代码只是简单地将first.xml和second.xml文件的文本内容倒在main.xml文件的底部。
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'))
我希望输出的内容类似于这样,将所有测试用例XML文本嵌入到testspecelement中的报告节点中:
<!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>
任何帮助都将不胜感激。谢谢,MikG。
testspecif
节点吗? - Padraic Cunningham