使用python的ElementTree合并xml文件

时间:2021-01-14 20:30:11

I need to merge two xml files on the third block of the xml. So, files A.xml and B.xml look like this:

我需要在xml的第三个块上合并两个xml文件。所以,文件A.xml和B.xml看起来像这样:

A.xml

<sample id="1">
<workflow value="x" version="1"/>
  <results>
   <result type="T">
      <result_data type="value" value="19"/>
      <result_data type="value" value="15"/>
      <result_data type="value" value="14"/>
      <result_data type="value" value="13"/>
      <result_data type="value" value="12"/>
    </result>
  </results>
</sample>

B.xml

<sample id="1">
<workflow value="x" version="1"/>
  <results>
   <result type="Q">
      <result_data type="value" value="11"/>
      <result_data type="value" value="21"/>
      <result_data type="value" value="13"/>
      <result_data type="value" value="12"/>
      <result_data type="value" value="15"/>
    </result>
  </results>
</sample>

I need to merge on 'results'

我需要合并'结果'

<sample id="1">
<workflow value="x" version="1"/>
  <results>
   <result type="T">
      <result_data type="value" value="19"/>
      <result_data type="value" value="15"/>
      <result_data type="value" value="14"/>
      <result_data type="value" value="13"/>
      <result_data type="value" value="12"/>
   </result>
   <result type="Q">
      <result_data type="value" value="11"/>
      <result_data type="value" value="21"/>
      <result_data type="value" value="13"/>
      <result_data type="value" value="12"/>
      <result_data type="value" value="15"/>
   </result>
  </results>
</sample>

What I have done so far is this:

到目前为止我所做的是:

import os, os.path, sys
import glob
from xml.etree import ElementTree

def run(files):
    xml_files = glob.glob(files +"/*.xml")
    xml_element_tree = None
    for xml_file in xml_files:
        # get root
        data = ElementTree.parse(xml_file).getroot()
        # print ElementTree.tostring(data)
        for result in data.iter('result'):
            if xml_element_tree is None:
                xml_element_tree = data 
            else:
                xml_element_tree.extend(result) 
    if xml_element_tree is not None:
        print ElementTree.tostring(xml_element_tree)

As you can see, I assign the initial xml_element_tree to data which has the heading etc, and then extend with 'result'. However, this gives me this:

如您所见,我将初始xml_element_tree分配给具有标题等的数据,然后使用'result'进行扩展。但是,这给了我这个:

<sample id="1">
<workflow value="x" version="1"/>
  <results>
   <result type="T">
      <result_data type="value" value="19"/>
      <result_data type="value" value="15"/>
      <result_data type="value" value="14"/>
      <result_data type="value" value="13"/>
      <result_data type="value" value="12"/>
   </result>
  </results>
   <result_data type="value" value="11"/>
      <result_data type="value" value="21"/>
      <result_data type="value" value="13"/>
      <result_data type="value" value="12"/>
      <result_data type="value" value="15"/>
   </result>
</sample>

where the results need to be at the bottom. Any help will be appreciated.

结果需要在底部。任何帮助将不胜感激。

1 个解决方案

#1


5  

Although this is mostly a duplicate and the answer can be found here, I already did this so i can share this python code:

虽然这大多是重复的,答案可以在这里找到,我已经这样做了所以我可以分享这个python代码:

import os, os.path, sys
import glob
from xml.etree import ElementTree

def run(files):
    xml_files = glob.glob(files +"/*.xml")
    xml_element_tree = None
    for xml_file in xml_files:
        data = ElementTree.parse(xml_file).getroot()
        # print ElementTree.tostring(data)
        for result in data.iter('results'):
            if xml_element_tree is None:
                xml_element_tree = data 
                insertion_point = xml_element_tree.findall("./results")[0]
            else:
                insertion_point.extend(result) 
    if xml_element_tree is not None:
        print ElementTree.tostring(xml_element_tree)

However this question contains another problem not present in the other post. The sample XML files are not valid XML so its not possible to have a XML tag with:

然而,这个问题包含另一个帖子中没有的另一个问题。示例XML文件不是有效的XML,因此无法使用XML标记:

<sample="1">
    ...
</sample>

is not possible change to something like:

不可能改为:

<sample id="1">
    ...
</sample>

#1


5  

Although this is mostly a duplicate and the answer can be found here, I already did this so i can share this python code:

虽然这大多是重复的,答案可以在这里找到,我已经这样做了所以我可以分享这个python代码:

import os, os.path, sys
import glob
from xml.etree import ElementTree

def run(files):
    xml_files = glob.glob(files +"/*.xml")
    xml_element_tree = None
    for xml_file in xml_files:
        data = ElementTree.parse(xml_file).getroot()
        # print ElementTree.tostring(data)
        for result in data.iter('results'):
            if xml_element_tree is None:
                xml_element_tree = data 
                insertion_point = xml_element_tree.findall("./results")[0]
            else:
                insertion_point.extend(result) 
    if xml_element_tree is not None:
        print ElementTree.tostring(xml_element_tree)

However this question contains another problem not present in the other post. The sample XML files are not valid XML so its not possible to have a XML tag with:

然而,这个问题包含另一个帖子中没有的另一个问题。示例XML文件不是有效的XML,因此无法使用XML标记:

<sample="1">
    ...
</sample>

is not possible change to something like:

不可能改为:

<sample id="1">
    ...
</sample>