将XML数据从XML文件添加到现有XML文件

时间:2014-01-15 22:07:21

标签: python xml elementtree

我是Python新手,只阅读Learn Python the Hard Way。但我认为这仍然超出了我的范围。我的技能是XML / XSL,而不是Python。我需要一些帮助才能开始。

概述:我需要将缺少的XML数据(addition.xml)添加到现有的XML文件(original.xml)中。

XML文件(缺少数据):( addition.xml)

<profile>
    <dog-list>
        <dog>
            <name>sally</dog>
            <age>1</age>
        </dog>
        <dog>
            <name>susie</dog>
            <age>12</age>
        </dog>
    </dog-list>
    <people-list>
        <person>
            <name>ue</name>
            <age>25</age>
            <gender>female</gender>
        </person>
    </people-list>
</profile>

上面的XML数据添加到此XML文件:(original.xml)

<profile>
    <cat-list>
        <cat>
            <name>foo></name>
        </cat>
        <cat>
            <name>bar</name>
            <age>3</age>
        </cat>
    </cat-list>
    <bird-list>
        <bird>
            <name>cricket</name>
            <age>2</age>
        </bird>
    </bird-list>
    <people-list>
        <person>
            <name>tyler</name>
            <age>26</age>
        </person>
    </people-list>
    <car-list>
        <car>
            <make>mitsubishi</make>
            <model>evo x</model>
            <year>2013</year>
        </car>
    </car-list>
</profile>

我的预期输出应为: - &gt;新的(original.xml)

<profile>
    <cat-list>
        <cat>
            <name>foo></name>
        </cat>
        <cat>
            <name>bar</name>
            <age>3</age>
        </cat>
    </cat-list>
    <dog-list>
        <dog>
            <name>sally</dog>
            <age>1</age>
        </dog>
        <dog>
            <name>susie</dog>
            <age>12</age>
        </dog>
    </dog-list>
    <bird-list>
        <bird>
            <name>cricket</name>
            <age>2</age>
        </bird>
    </bird-list>
    <people-list>
        <person>
            <name>tyler</name>
            <age>26</age>
        </person>
        <person>
            <name>ue</name>
            <age>25</age>
            <gender>female</gender>
        </person>
    </people-list>
    <car-list>
        <car>
            <make>mitsubishi</make>
            <model>evo x</model>
            <year>2013</year>
        </car>
    </car-list>
</profile>

这里发生的是原始文件中缺少来自addition.xml的数据。如何将additional.xml中的数据添加到original.xml中,而不是创建新文件,覆盖它。

我遍布google和stackoverflow。我知道我可以使用ElementTree,但我有一个最模糊的想法,就是如何创建这个结果。

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:1)

您的要求不允许使用通用合并程序(例如您链接的程序),但这是一个可能适合您的程序。

用法:./ program.py original.xml addition.xml

#! /usr/bin/python2

import sys
from lxml import etree

result = etree.Element('root')
parser = etree.XMLParser(remove_blank_text=True)

# Add each file to the tree
for xmlfile in sys.argv[1:]:
  with open(xmlfile) as xmlfile:
    btree = etree.parse(xmlfile, parser)
  # Ensure that the resulting tree has the right root
  result.tag = btree.getroot().tag
  # Consider each 2nd-level item
  for bchild in btree.xpath("/*/*"):
    tags = result.xpath("./%s"%bchild.tag)
    if len(tags) == 0:
      # Add <dog-list>, for example
      #print "adding %s to %s"%(bchild.tag, result.tag)
      result.append(bchild)
    else:
      for bgrandchild in bchild:
        # add <dog>, for example
        #print "adding %s to %s"%(bgrandchild.tag, tags[0].tag)
        tags[0].append(bgrandchild)

with open("output.xml", "w") as output:
  output.write(etree.tostring(result, pretty_print = True))