使用没有外部库的嵌套元素合并xml文件

Inb*_*ose 14 python xml elementtree python-2.7

我试图使用Python将多个XML文件合并在一起,而不是外部库.XML文件具有嵌套元素.

示例文件1:

<root>
  <element1>textA</element1>
  <elements>
    <nested1>text now</nested1>
  </elements>
</root>
Run Code Online (Sandbox Code Playgroud)

示例文件2:

<root>
  <element2>textB</element2>
  <elements>
    <nested1>text after</nested1>
    <nested2>new text</nested2>
  </elements>
</root>
Run Code Online (Sandbox Code Playgroud)

我想要的是:

<root>
  <element1>textA</element1>    
  <element2>textB</element2>  
  <elements>
    <nested1>text after</nested1>
    <nested2>new text</nested2>
  </elements>  
</root>  
Run Code Online (Sandbox Code Playgroud)

我尝试过的:

这个答案.

from xml.etree import ElementTree as et
def combine_xml(files):
    first = None
    for filename in files:
        data = et.parse(filename).getroot()
        if first is None:
            first = data
        else:
            first.extend(data)
    if first is not None:
        return et.tostring(first)
Run Code Online (Sandbox Code Playgroud)

我得到了什么:

<root>
  <element1>textA</element1>
  <elements>
    <nested1>text now</nested1>
  </elements>
  <element2>textB</element2>
  <elements>
    <nested1>text after</nested1>
    <nested2>new text</nested2>
  </elements>
</root>
Run Code Online (Sandbox Code Playgroud)

我希望你能看到并理解我的问题.我正在寻找一个合适的解决方案,任何指导都会很精彩.

为了澄清问题,使用我当前的解决方案,嵌套元素不会合并.

jad*_*k94 22

您发布的代码所做的是组合所有元素,无论具有相同标记的元素是否已存在.因此,您需要迭代元素并按照您认为合适的方式手动检查和组合它们,因为它不是处理XML文件的标准方法.我无法解释它比代码更好,所以在这里,或多或少评论:

from xml.etree import ElementTree as et

class XMLCombiner(object):
    def __init__(self, filenames):
        assert len(filenames) > 0, 'No filenames!'
        # save all the roots, in order, to be processed later
        self.roots = [et.parse(f).getroot() for f in filenames]

    def combine(self):
        for r in self.roots[1:]:
            # combine each element with the first one, and update that
            self.combine_element(self.roots[0], r)
        # return the string representation
        return et.tostring(self.roots[0])

    def combine_element(self, one, other):
        """
        This function recursively updates either the text or the children
        of an element if another element is found in `one`, or adds it
        from `other` if not found.
        """
        # Create a mapping from tag name to element, as that's what we are fltering with
        mapping = {el.tag: el for el in one}
        for el in other:
            if len(el) == 0:
                # Not nested
                try:
                    # Update the text
                    mapping[el.tag].text = el.text
                except KeyError:
                    # An element with this name is not in the mapping
                    mapping[el.tag] = el
                    # Add it
                    one.append(el)
            else:
                try:
                    # Recursively process the element, and update it in the same way
                    self.combine_element(mapping[el.tag], el)
                except KeyError:
                    # Not in the mapping
                    mapping[el.tag] = el
                    # Just add it
                    one.append(el)

if __name__ == '__main__':
    r = XMLCombiner(('sample1.xml', 'sample2.xml')).combine()
    print '-'*20
    print r
Run Code Online (Sandbox Code Playgroud)