python写xml保留注释_python 注释xml的元素

该代码实现了一个Python脚本,用于在XML配置文件中切换HTTP和HTTPS连接器的状态。它从输入XML路径获取第一个web系统元素,根据指定的类型(http或https)切换连接器,然后将更改写入输出XML路径。脚本首先注释掉当前不使用的连接器,然后取消注释所需的连接器,从而实现连接器类型的切换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

code is:

#!/usr/bin/env python

from xml.dom importminidomimportsys'''get the first web system element, like:

Note, should have only one web system element, if have multiple, the other will be ignored.'''

defgetWebSystemElement(xmlPath):

doc=minidom.parse(xmlPath).documentElement

subsystems= doc.getElementsByTagName('subsystem')for subsystem insubsystems:if subsystem.getAttribute("xmlns").startswith("urn:jboss:domain:web:"):returnsubsystem;'''change to https type

uncomment HTTPS, make HTTPS effective

comment HTTP, make HTTP uneffective'''

defchangeToHttps(webSystem):#comment the HTTP

for node inwebSystem.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 inwebSystem.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 inelements.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'''

defchangeToHttp(webSystem):#comment the HTTPS

for node inwebSystem.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 inwebSystem.childNodes:if node.nodeType ==node.COMMENT_NODE:#parse the content in the comment to httpsElement

elements =minidom.parseString(node.data)for element inelements.childNodes:if element.nodeType == node.ELEMENT_NODE and element.tagName == "connector" and element.getAttribute("name") == "http":

node.parentNode.replaceChild(element, node)else:pass

defswitchWithException(inputPath, outputPath, hType):if hType == "http":

webSystem=getWebSystemElement(inputPath)

changeToHttp(webSystem)

with open(outputPath,'w') as wf:

c=webSystem.ownerDocument.toxml()

wf.write(c)returnTrueelif hType == "https":

webSystem=getWebSystemElement(inputPath)

changeToHttps(webSystem)

with open(outputPath,'w') as wf:

c=webSystem.ownerDocument.toxml()

wf.write(c)returnTrueelse:print "Type" + hType + "should be http or https"

returnFalse'''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'''

defswitch(inputPath, outputPath, hType):try:returnswitchWithException(inputPath, outputPath, hType)exceptBaseException as e:printereturnFalse'''./switch_http_htts.py inputPath outputPath '''

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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值