从命令行合并多个XML文件 [英] Merge multiple XML files from command line
问题描述
我有几个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 omitpython
in the command linexmlcombine.py !(combined).xml > combined.xml
: Collects all XML files except the output, but requires bash'sextglob
optionxmlcombine.py *.xml | sponge combined.xml
: Collects everything incombined.xml
as well, but requires thesponge
programimport lxml.etree as ElementTree
: Uses a potentially faster XML parser
这篇关于从命令行合并多个XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!