Created
September 5, 2012 15:49
-
-
Save jtriley/3638809 to your computer and use it in GitHub Desktop.
Combine similar XML files into one document
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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