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)
python 注释xml的元素的更多相关文章
- Python之xml文档及配置文件处理(ElementTree模块、ConfigParser模块)
本节内容 前言 XML处理模块 ConfigParser/configparser模块 总结 一.前言 我们在<中我们描述了Python数据持久化的大体概念和基本处理方式,通过这些知识点我们已经 ...
- 【转】Python之xml文档及配置文件处理(ElementTree模块、ConfigParser模块)
[转]Python之xml文档及配置文件处理(ElementTree模块.ConfigParser模块) 本节内容 前言 XML处理模块 ConfigParser/configparser模块 总结 ...
- 【Python】xml 解析
1. XML:指可扩展标记语言,是一种标记语言,用于存储数据和传输数据,但没有像HTML那样具有预定义标签,需要程序猿自定义标签 2. XML的解析:读取XML数据结构中的某些信息,比如读取书的属性 ...
- python 处理xml
XML XML指可扩展标记语言(Extensible Markup Language) XML被设计用于结构化.存储和传输数据 XML是一种标记语言,很类似于HTML XML没有像HTML那样 ...
- python读取xml文件
关于python读取xml文章很多,但大多文章都是贴一个xml文件,然后再贴个处理文件的代码.这样并不利于初学者的学习,希望这篇文章可以更通俗易懂的教如何使用python 来读取xml 文件. 什么是 ...
- python 解析XML python模块xml.dom解析xml实例代码
分享下python中使用模块xml.dom解析xml文件的实例代码,学习下python解析xml文件的方法. 原文转自:http://www.jbxue.com/article/16587.html ...
- python解析xml之lxml
虽然python解析xml的库很多,但是,由于lxml在底层是用C语言实现的,所以lxml在速度上有明显优势.除了速度上的优势,lxml在使用方面,易用性也非常好.这里将以下面的xml数据为例,介绍l ...
- python写xml文件
为了便于后续的读取处理,这里就将信息保存在xml文件中,想到得到的文件如下: 1 <?xml version="1.0" encoding="utf-8" ...
- Python实现XML文件解析
1. XML简介 XML(eXtensible Markup Language)指可扩展标记语言,被设计用来传输和存储数据,已经日趋成为当前许多新生技术的核心,在不同的领域都有着不同的应用.它是web ...
随机推荐
- 封装removeClass()
<div class="box haha xixi">123</div> <script> function removeClass(eleme ...
- android hook 框架 libinject2 如何实现so注入
Android so注入-libinject2 简介.编译.运行 Android so注入-libinject2 如何实现so注入 Android so注入-Libinject 如何实现so注入 A ...
- 内核中的内存申请:kmalloc、vmalloc、kzalloc、kcalloc、get_free_pages【转】
转自:http://www.cnblogs.com/yfz0/p/5829443.html 在内核模块中申请分配内存需要使用内核中的专用API:kmalloc.vmalloc.kzalloc.kcal ...
- msvc交叉编译:使用vcvarsall.bat设置命令行编译环境
一直以来我只知道vc设置命令行编译环境的批处理命令是%VS140COMNTOOLS%/Common7/Tools下的vsvars32.bat,(%VS140COMNTOOLS%为定义vs2015公共工 ...
- hdu 1811(缩点+拓扑排序+并查集)
Rank of Tetris Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- 用了GradientDrawable后,当点击控件时,控件大小发生变化
android新手:发现一个很奇怪的问题,用了GradientDrawable后,当点击控件时,程序自动使我的一些控件大小保持一致,为什么呢,我就是不想它们保持一致啊 改了好久好久:GradientD ...
- poj2104(划分树模板)
poj2104 题意 给出一个序列,每次查询一个区间,要求告诉这个区间排序后的第k个数. 分析 划分树模板,O(mlogn). 建树.根据排序之后的数组,对于一个区间,找到中点的数,将整个区间分为左右 ...
- (寒假集训)洛谷 P2058 海港
小K是一个海港的海关工作人员,每天都有许多船只到达海港,船上通常有很多来自不同国家的乘客. 小K对这些到达海港的船只非常感兴趣,他按照时间记录下了到达海港的每一艘船只情况:对于第i艘到达的船,他记录了 ...
- 网页结构——head标签内
之前写网页都很标准的格式,最近一个项目出现了页面闪动等一系列问题[项目不是前后端分离], 所以这边有后台的功劳,有部分后台是不管你页面结构的,在他们操作的时候可能会在,你的head内meta前加内联c ...
- luogu P1195 口袋的天空
题目背景 小杉坐在教室里,透过口袋一样的窗户看口袋一样的天空. 有很多云飘在那里,看起来很漂亮,小杉想摘下那样美的几朵云,做成棉花糖. 题目描述 给你云朵的个数N,再给你M个关系,表示哪些云朵可以连在 ...