Skip to content

Instantly share code, notes, and snippets.

@BenHigginbottom
Created September 1, 2015 20:55
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 BenHigginbottom/2f91ac963b71e6444d01 to your computer and use it in GitHub Desktop.
Save BenHigginbottom/2f91ac963b71e6444d01 to your computer and use it in GitHub Desktop.
Comment out a namespace in an xmlfile, breaking any other comments within the namespace first
from xml.dom import minidom
import os, time, shutil
xmlfile=('<xmlfile here')
resultFile='result.xml'
commentSub=('<namespacehere>')
now=str(int(time.time()))
bkup=(xmlfile+now)
shutil.copy2(xmlfile, bkup)
xmldoc = minidom.parse(xmlfile)
itemlist = xmldoc.getElementsByTagName('subsystem')
for s in itemlist:
if commentSub in s.attributes['xmlns'].value:
commentText = s.toxml()
commentText = commentText.replace('--', '- -')
s.parentNode.insertBefore(xmldoc.createComment(commentText), s)
s.parentNode.removeChild(s)
file = open("result.xml", "wb")
xmldoc.writexml(file)
file.write('\n')
file.close()
shutil.copy2(resultFile, xmlfile)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment