我正在使用以下代码(在stackexchange上找到)合并XML文件:
#!/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,encoding="utf8")
if __name__ == "__main__":
run(sys.argv[1:])
但是当字符串在XML中包含换行符时,例如
<item text="blabla line 1
blabla line 3"/>
合并文件的过程将删除换行符。
是否可以通过合并过程合并这些文件,而使换行符保留在原处?我的工作流程中的后续流程会利用它们。