python - 使用 Python 将 XML 合并到父 XML 文件中

标签 python xml python-3.x

我发现了几个在 SO 上使用 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 的位置;这些如下所示。

(第一个.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 文本都嵌入在 testspecif 元素内的报告节点中:

<!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>

如有任何帮助,我们将不胜感激。谢谢,米克格

最佳答案

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)

您的示例输入会给您:

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

关于python - 使用 Python 将 XML 合并到父 XML 文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37852738/

相关文章:

python - 从现有数据框创建新的数据框而不缺少值

android - 微调器下方的下划线未与 EditText 的下划线对齐

Android XML : Good examples to use gravity rather than layout_gravity

python - 如何通过 "Duck Typing"区分元组和简单字符串?

python - Scikit Learn SGDClassifier Partial_Fit 错误

python - matplotlib colororder 和 twinx

xml - 你如何解释这个 URL 时间格式?

python - 从姓名列表中生成学生对,每周新对

python - 如何使用循环重写二维数组切片(打开cv格式)

python - Pygame 不会检测同一类中的列表