Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Combine similar XML files into one document
# From: http://tentacles.posterous.com/43038706
# Take a bunch of XML files on the command line and merge them into
# one big XML document.
#
# The root element will come from the first document; the root elements of
# subsequent documents will be lost, as will anything outside the root
# (comments and whatnot).
import sys
import libxml2
doc = None
root = None
for i in range(1, len(sys.argv)):
newdoc = libxml2.parseFile(sys.argv[i])
newroot = newdoc.getRootElement()
if newroot:
if not root:
# first document with a root element
doc = newdoc
root = newroot
else:
# merge this into previous document
root.addChildList(newroot.children.copyNodeList())
newdoc.freeDoc()
if doc:
print doc
doc.freeDoc()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment