使用python的ElementTree合并xml文件 [英] merging xml files using python's ElementTree
问题描述
我需要在xml的第三块上合并两个xml文件.因此,文件A.xml和B.xml如下所示:
I need to merge two xml files on the third block of the xml. So, files A.xml and B.xml look like this:
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>
我需要合并结果"
<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)
如您所见,我将初始xml_element_tree分配给具有标题等的数据,然后以'result'扩展.但是,这给了我这个:
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:
<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.
推荐答案
Although this is mostly a duplicate and the answer can be found here, I already did this so i can share this python code:
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)
但是,此问题包含另一个帖子中未提及的另一个问题.示例XML文件不是有效的XML,因此无法使用带有以下标记的XML标签:
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:
<sample="1">
...
</sample>
不可能更改为以下内容:
is not possible change to something like:
<sample id="1">
...
</sample>
这篇关于使用python的ElementTree合并xml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!