Skip to content

Instantly share code, notes, and snippets.

@jtriley
Created September 5, 2012 15:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jtriley/3638809 to your computer and use it in GitHub Desktop.
Save jtriley/3638809 to your computer and use it in GitHub Desktop.
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