Created
September 1, 2015 20:55
-
-
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
This file contains 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 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