python - 在 Python 中使用 Element Tree 合并 xml 文件

标签 python xml xpath merge elementtree

我正在尝试合并两个 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)

请帮忙

最佳答案

我过去做过类似的事情的一种方法是创建一个 xml 文档,然后附加您要查找的值。我不相信有办法“合并”它们

xml = ET.fromstring("<book></book>")
document = ET.parse(tempFile)
childNodeList = document.findall(xpathQuery)
for node in childNodeList: 
   xml.append(node)

关于python - 在 Python 中使用 Element Tree 合并 xml 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17867583/

相关文章:

Python:从多个 xlsx 文件创建多个 xml 文件

python - xpath - 选择父级,其中子级具有带有命名空间字典的特定属性值

python - 如何通过 XPath 查找具有两个可能类名的元素?

python - 在 Odoo 8 上设置树字体

python - Pandas 基于列合并 csv

python - 使用 --oauth_refresh_token 更新时 appcfg.py 返回未经授权的客户端

python - 无法使用 pip 安装requirements.txt

json - JSON 和 YAML 是否有相当于 DTD 或 XML 架构的工具?

xml - 记录导致 XML 解析中断?

java - java中xpath表达式动态赋值