从命令行合并多个XML文件 [英] Merge multiple XML files from command line

查看:290
本文介绍了从命令行合并多个XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个xml文件。它们都具有相同的结构,但是由于文件大小而分裂。所以,假设我有 A.xml B.xml C.xml D.xml ,并希望将它们组合/合并到 combined.xml 命令行工具。



A.xml

 < products& 
< product id =1234>< / product>
...
< / products>

B.xml

 < products> 
< product id =5678>< / product>
...
< / products>

等。

解决方案

高科技回答:



将此Python脚本另存为xmlcombine.py:

 #!/ usr / bin / env python 
import sys
from xml.etree import ElementTree

def run(files):
first = None
文件中的文件名:
data = ElementTree.parse(filename).getroot()
如果第一个是None:
first = data
else
first.extend(data)
如果第一个不是None:
print ElementTree.tostring(first)

如果__name__ ==__main__:
run(sys.argv [1:])

要组合文件, p>

  python xmlcombine.py?.xml> combine.xml 

要进一步增强,请考虑使用:




  • chmod + x xmlcombine.py
    允许您省略 python

  • xmlcombine.py!(combined).xml> combine.xml
    收集除输出之外的所有XML文件,但需要bash的 extglob 选项


  • xmlcombine.py * .xml |海绵combine.xml
    收集 combined.xml 中的所有内容,但需要 sponge 程序


  • import lxml.etree as ElementTree
    潜在更快的XML解析器



I have several xml files. They all have the same structure, but were splitted due to file size. So, let's say I have A.xml, B.xml, C.xml and D.xml and want to combine/merge them to combined.xml, using a command line tool.

A.xml

<products>
    <product id="1234"></product>
    ...
</products>

B.xml

<products>
  <product id="5678"></product>
  ...
</products>

etc.

解决方案

High-tech answer:

Save this Python script as xmlcombine.py:

#!/usr/bin/env python
import sys
from xml.etree import ElementTree

def run(files):
    first = None
    for filename in files:
        data = ElementTree.parse(filename).getroot()
        if first is None:
            first = data
        else:
            first.extend(data)
    if first is not None:
        print ElementTree.tostring(first)

if __name__ == "__main__":
    run(sys.argv[1:])

To combine files, run:

python xmlcombine.py ?.xml > combined.xml

For further enhancement, consider using:

  • chmod +x xmlcombine.py: Allows you to omit python in the command line

  • xmlcombine.py !(combined).xml > combined.xml: Collects all XML files except the output, but requires bash's extglob option

  • xmlcombine.py *.xml | sponge combined.xml: Collects everything in combined.xml as well, but requires the sponge program

  • import lxml.etree as ElementTree: Uses a potentially faster XML parser

这篇关于从命令行合并多个XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆