zoukankan      html  css  js  c++  java
  • python 注释xml的元素

    use xml.dom.minidom 注释xml元素和去除xml注释。


    code is:

    #
    !/usr/bin/env python from xml.dom import minidom import sys ''' get the first web system element, like: <subsystem xmlns="urn:jboss:domain:web:..."> Note, should have only one web system element, if have multiple, the other will be ignored. ''' def getWebSystemElement(xmlPath): doc = minidom.parse(xmlPath).documentElement subsystems = doc.getElementsByTagName('subsystem') for subsystem in subsystems: if subsystem.getAttribute("xmlns").startswith("urn:jboss:domain:web:"): return subsystem; ''' change to https type uncomment HTTPS, make HTTPS effective comment HTTP, make HTTP uneffective ''' def changeToHttps(webSystem): # comment the HTTP for node in webSystem.childNodes: if node.nodeType == node.ELEMENT_NODE and node.nodeName == "connector" and node.getAttribute("name") == "http": comment = node.ownerDocument.createComment(node.toxml()) node.parentNode.replaceChild(comment, node) else: pass # uncomment the HTTPS for node in webSystem.childNodes: if(node.nodeType == node.COMMENT_NODE): # parse the comment content to element element = minidom.parseString(node.data).firstChild elements = minidom.parseString(node.data) for element in elements.childNodes: if element.nodeType == node.ELEMENT_NODE and element.tagName == "connector" and element.getAttribute("name") == "https": node.parentNode.replaceChild(element, node) else: pass ''' change to http type uncomment HTTP, make HTTP effective comment HTTPS, make HTTPS uneffective ''' def changeToHttp(webSystem): # comment the HTTPS for node in webSystem.childNodes: if node.nodeType == node.ELEMENT_NODE and node.tagName == "connector" and node.getAttribute("name") == "https": # create commented httpsElement comment = node.ownerDocument.createComment(node.toxml()) node.parentNode.replaceChild(comment, node) else: pass # uncomment the HTTP for node in webSystem.childNodes: if node.nodeType == node.COMMENT_NODE: # parse the content in the comment to httpsElement elements = minidom.parseString(node.data) for element in elements.childNodes: if element.nodeType == node.ELEMENT_NODE and element.tagName == "connector" and element.getAttribute("name") == "http": node.parentNode.replaceChild(element, node) else: pass def switchWithException(inputPath, outputPath, hType): if hType == "http": webSystem = getWebSystemElement(inputPath) changeToHttp(webSystem) with open(outputPath, 'w') as wf: c = webSystem.ownerDocument.toxml() wf.write(c) return True elif hType == "https": webSystem = getWebSystemElement(inputPath) changeToHttps(webSystem) with open(outputPath, 'w') as wf: c = webSystem.ownerDocument.toxml() wf.write(c) return True else: print "Type " + hType + " should be http or https" return False ''' switch the http/https type in the standalone.xml inputPath: the input path of standalone.xml, include the file name outputPath: the output path of standalone.xml, include the file name ''' def switch(inputPath, outputPath, hType): try: return switchWithException(inputPath, outputPath, hType) except BaseException as e: print e return False ''' ./switch_http_htts.py inputPath outputPath <http or https> ''' if __name__ == '__main__': if len(sys.argv) != 4 : print 'Invalid length of parameter list' sys.exit(1) if switch(sys.argv[1], sys.argv[2],sys.argv[3]): sys.exit(0) else: sys.exit(1)
  • 相关阅读:
    VIM技巧, .vimrc文件
    vSphere Client克隆虚拟机
    vSphere、 ESXi、Vcenter、vSphere Client关系
    消耗系统内存
    zabbix2.4汉化
    普通用户执行ansible权限被拒绝
    putty秘钥转换成xhell支持的格式
    zbb20170922 mysql 字符集设置 比较 utf8_general_ci、utf8_unicode_ci和utf8_bin的区别
    zbb20170920 页面调用qq
    zbb20170919 tomcat 8 启动异常 Could not publish server configuration for Tomcat v8.0 Server at localhost. Multiple Contexts have a path of "A".
  • 原文地址:https://www.cnblogs.com/lpthread/p/3461827.html
Copyright © 2011-2022 走看看