Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Combine similar XML files into one document
# From:
# 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
# merge this into previous document
if doc:
print doc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment