I'm using the Python elemntTree in order to merge a couple of xml files into one. This is the code I wrote for this:

xml_files = glob.glob('*_to_merge.xml')
xml_element_tree = None
for xml_file in xml_files:
    data = ElementTree.parse(xml_file).getroot()
    for result in data.iter('root'):
        if xml_element_tree is None:
            xml_element_tree = data 
            insertion_point = xml_element_tree.findall("./insert")[0]
        else:
            insertion_point.extend(result) 

op_res = ElementTree.tostring(xml_element_tree).decode("utf-8")

file = 'merged.xml'
with open(file, 'w') as outfile:   
    outfile.write(op_res)  

The file that I get does include all the files to be merged. The problem is that those contain processing instruction <? ….. ?> which unfortunately do not appear in the resulting xml anymore. So they have been ignored/removed from the code.

Could anyone help me to find a way to merge my files without losing the processing instructions.

Thank you.

0 Answers