python将注释写入xml_python 注释xml的元素
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)
python将注释写入xml_python 注释xml的元素相关推荐
- 用python去除SQL中的注释
在看到这个标题时候肯定有人会想,我写SQL直接在数据库工具上执行就行了啊,工具会自动识别注释的,就是不用工具,把SQL写到存储过程里,数据库也会识别注释不执行的,干嘛非要去掉,费力不讨好. 其实是最近 ...
- python整段注释_python段注释
广告关闭 腾讯云11.11云上盛惠 ,精选热门产品助力上云,云服务器首年88元起,买的越多返的越多,最高返5000元! 初学python习得注释方法如下:#我是注释print(hello) 我是多行注 ...
- python的注释符_python 注释符
广告关闭 腾讯云11.11云上盛惠 ,精选热门产品助力上云,云服务器首年88元起,买的越多返的越多,最高返5000元! 相比于行注释符的多样,块注释符更加是让人眼花缭乱:? 大多数写法是我从未见过的, ...
- python语言中如何使用注释
每一种计算机语言都有自己的注释方式,我们知道注释的作用是解释这些代码,方便程序员以后的检查和修改.而且注释的一部分在运行程序的过程中不起作用,也不会显示出来.下面我们将为大家介绍,在python语言中 ...
- Python 为什么用 # 号作注释符?
点击上方"小白学视觉",选择加"星标"或"置顶" 重磅干货,第一时间送达 作者:豌豆花下猫 来源:Python猫 关于编程语言中的注释,其重 ...
- python为什么用号做注释符_Python为什么用#号作注释符?
Apple iPhone 11 (A2223) 128GB 黑色 移动联通电信4G手机 双卡双待 4999元包邮 去购买 > 关于编程语言中的注释,其重要性基本上已为大家所共识. 很多人学习py ...
- python注释_Python头条:python基础知识了解___注释、变量、Debug
注释 作用 通过用自己熟悉的语言,在程序中对某些代码进行标注说明,这就是注释的作用,能够大大增强程序的可读性. 分类与语法 单行注释 只注释一行内容,语法如下: # 注释内容 多行注释 可以注释多行内 ...
- python编码注释和平台注释_python注释是什么意思
注释即对程序代码的解释,在写程序时需适当使用注释,以方便自己和他人理解程序各部分的作用.在执行时,它会被Python解释器忽略,因此不会影响程序的执行. Python支持单行注释与多行注释,具体如下所 ...
- python语言的两种注释方法_python编程时添加中文注释的方法
python编程时添加中文注释的方法 发布时间:2020-08-24 17:09:52 来源:亿速云 阅读:77 作者:小新 这篇文章主要介绍python编程时添加中文注释的方法,文中介绍的非常详细, ...
最新文章
- Linux/docker下oracle开启监听,开启自动启动
- vue项目-封装API接口
- C# 视频监控系列(5):客户端——给服务器端发送字符串和录像(数据捕获)
- 计算机英语复习,计算机英语期末复习
- python 代码转程序_python将代码转换成网页
- php进销存 带apk,php进销存配送管理系统,支持h5/ios/android/微信小程序
- 连招 横版 flash 游戏_街机游戏中的无限连究竟有多变态?有种对决叫作没开始就结束了!...
- c++ winpcap开发(10)
- android关联权限,基于关联规则的Android权限研究及应用
- 怎么让同网络计算机强制关机,知道局域网ip怎么关机
- Linux 系统调优相关工具
- git flow使用
- eda技术试卷_EDA技术及应用试卷D含答案
- 图片转字符画(已打包)
- 【Linux】 - Linux中查看命令文档的命令
- python 拼接 遥感影像_如何用Python| 制作遥感影像拼接
- Google Android 原生Rom 下载地址及刷机教程--Factory Images for Nexus and Pixel Devices
- 感叹号的形状像什么_三个感叹号的句子
- svn propset svn:ignore
- 具有多孔光纤的偏振分束器
热门文章
- 组原-OS-政治截图
- Python爬虫获取文章的标题及你的博客的阅读量,评论量。所有数据写入本地记事本。最后输出你的总阅读量!
- 图表示学习+图神经网络:破解AI黑盒,揭示万物奥秘的钥匙!
- PrestaShop 网站后台配置(六)
- Angular5学习笔记 - 虚拟RestfulApi配置与使用(六)
- [Head First设计模式]身边的设计模式——适配器模式
- Mac下Android配置及unity3d的导出Android
- 性能测试应用领域分析
- android地图定位
- Silverlight+WCF 新手实例 象棋 主界面-实时聊天区(二十五)