Я пытаюсь объединить два файла xml. Файлы имеют одинаковую общую структуру, но отличаются деталями.
файл1.xml:
<book>
<chapter id="113">
<sentence id="1">
<word id="128160">
<POS Tag="V"/>
<grammar type="STEM"/>
<Aspect type="IMPV"/>
<Number type="S"/>
</word>
<word id="128161">
<POS Tag="V"/>
<grammar type="STEM"/>
<Aspect type="IMPF"/>
</word>
</sentence>
<sentence id="2">
<word id="128162">
<POS Tag="P"/>
<grammar type="PREFIX"/>
<Tag Tag="bi+"/>
</word>
</sentence>
</chapter>
</book>
файл2.xml:
<book>
<chapter id="113">
<sentence id="1">
<word id="128160">
<concept English="joke"/>
</word>
<word id="128161">
<concept English="romance"/>
</word>
</sentence>
<sentence id="2">
<word id="128162">
<concept English="happiness"/>
</word>
</sentence>
</chapter>
</book>
Желаемый результат:
<book>
<chapter id="113">
<sentence id="1">
<word id="128160">
<concept English="joke"/>
<POS Tag="V"/>
<grammar type="STEM"/>
<Aspect type="IMPV"/>
<Number type="S"/>
</word>
<word id="128161">
<concept English="romance"/>
<POS Tag="V"/>
<grammar type="STEM"/>
<Aspect type="IMPF"/>
</word>
</sentence>
<sentence id="2">
<word id="128162">
<concept English="happiness"/>
<POS Tag="P"/>
<grammar type="PREFIX"/>
<Tag Tag="bi+"/>
</word>
</sentence>
</chapter>
</book>
Хорошо, я попытался сделать это в пути, но не получил желаемого результата:
import os, os.path, sys
import glob
from xml.etree import ElementTree
output = open('merge.xml','w')
files="sample"
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 word in data.iter('word'):
if xml_element_tree is None:
xml_element_tree = data
insertion_point = xml_element_tree.findall("book/chapter/sentence/word/*")
else:
insertion_point.extend(word)
if xml_element_tree is not None:
print>>output, ElementTree.tostring(xml_element_tree)
пожалуйста, любая помощь