import xml.etree.ElementTree as ET
def merge_xml_files(input_files, output_file):
# Create the root element for the merged XML document
merged_root = ET.Element("merged_data")
# Iterate over each input XML file
for file_path in input_files:
# Parse the XML file
tree = ET.parse(file_path)
root = tree.getroot()
# Iterate over the elements in the XML file and append them to the merged document
for elem in root:
merged_root.append(elem)
# Create a new XML tree with the merged root element
merged_tree = ET.ElementTree(merged_root)
# Write the merged XML tree to the output file
merged_tree.write(output_file, encoding="utf-8", xml_declaration=True)
# Example usage:
input_files = ["file1.xml", "file2.xml"] # List of input XML files to merge
output_file = "merged.xml" # Output file for the merged XML
merge_xml_files(input_files, output_file)