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的元素的更多相关文章

  1. Python之xml文档及配置文件处理(ElementTree模块、ConfigParser模块)

    本节内容 前言 XML处理模块 ConfigParser/configparser模块 总结 一.前言 我们在<中我们描述了Python数据持久化的大体概念和基本处理方式,通过这些知识点我们已经 ...

  2. 【转】Python之xml文档及配置文件处理(ElementTree模块、ConfigParser模块)

    [转]Python之xml文档及配置文件处理(ElementTree模块.ConfigParser模块) 本节内容 前言 XML处理模块 ConfigParser/configparser模块 总结 ...

  3. 【Python】xml 解析

    1. XML:指可扩展标记语言,是一种标记语言,用于存储数据和传输数据,但没有像HTML那样具有预定义标签,需要程序猿自定义标签 2. XML的解析:读取XML数据结构中的某些信息,比如读取书的属性 ...

  4. python 处理xml

    XML XML指可扩展标记语言(Extensible Markup Language) XML被设计用于结构化.存储和传输数据 XML是一种标记语言,很类似于HTML XML没有像HTML那样 ...

  5. python读取xml文件

    关于python读取xml文章很多,但大多文章都是贴一个xml文件,然后再贴个处理文件的代码.这样并不利于初学者的学习,希望这篇文章可以更通俗易懂的教如何使用python 来读取xml 文件. 什么是 ...

  6. python 解析XML python模块xml.dom解析xml实例代码

    分享下python中使用模块xml.dom解析xml文件的实例代码,学习下python解析xml文件的方法. 原文转自:http://www.jbxue.com/article/16587.html ...

  7. python解析xml之lxml

    虽然python解析xml的库很多,但是,由于lxml在底层是用C语言实现的,所以lxml在速度上有明显优势.除了速度上的优势,lxml在使用方面,易用性也非常好.这里将以下面的xml数据为例,介绍l ...

  8. python写xml文件

    为了便于后续的读取处理,这里就将信息保存在xml文件中,想到得到的文件如下: 1 <?xml version="1.0" encoding="utf-8" ...

  9. Python实现XML文件解析

    1. XML简介 XML(eXtensible Markup Language)指可扩展标记语言,被设计用来传输和存储数据,已经日趋成为当前许多新生技术的核心,在不同的领域都有着不同的应用.它是web ...

随机推荐

  1. [bzoj3270] 博物馆 [期望+高斯消元]

    题面 传送门 思路 本题的点数很少,只有20个 考虑用二元组$S=(u,v)$表示甲在$u$点,乙在$v$点的状态 那么可以用$f(S)$表示状态$S$出现的概率 不同的$f$之间的转移就是通过边 转 ...

  2. HDU 1007 Quoit Design | 平面分治

    暂鸽 #include<cstdio> #include<algorithm> #include<cstring> #include<cmath> #d ...

  3. 牛客 NOIp模拟1 T3 保护 解题报告

    保护 题目描述 \(C\)国有\(n\)个城市,城市间通过一个树形结构形成一个连通图.城市编号为\(1\)到\(n\),其中\(1\)号城市为首都.国家有\(m\)支军队,分别守卫一条路径的城市.具体 ...

  4. 一种简单高效的音频降噪算法示例(附完整C代码)

    近期比较忙, 抽空出来5.1开源献礼. 但凡学习音频降噪算法的朋友,肯定看过一个算法. <<语音增强-理论与实践>> 中提及到基于对数的最小均方误差的降噪算法,也就是LogMM ...

  5. 快速激活最新JetBrains公司系列产品包括最新的phpstorm10

    快速激活最新JetBrains公司系列产品包括最新的phpstorm10 IntelliJ IDEA开源社区 提供了如下通用激活方法: 注册时选择License server 然后输入框填写:http ...

  6. Vitamio介绍及使用

    一.Vitamio介绍 1.1 Vitamio是什么? Vitamio是Android平台视音频播放组件,支持播放几乎格式的视频以及主流网络视频流(http/rtsp/mms等),详细的中文介绍: 这 ...

  7. UVa10891 Game of Sum

    给定n个数字,A和B可以从这串数字的两端任意选数字,一次只能从一端选取.两人都采用最优策略,A先手,问A和B各自得到数字的和的差值最大为多少? 区间DP F[i][j]表示区间i~j内A能得到的最大数 ...

  8. awk 二

    节详细介绍awk内置函数,主要分以下3种类似:算数函数.字符串函数.其它一般函数.时间函数 一.算术函数: 以下算术函数执行与 C 语言中名称相同的子例程相同的操作: 函数名 说明 atan2( y, ...

  9. java基础练习 15

    import java.util.Scanner; public class Fiftheen { /*有n个人围成一圈,顺序排号.从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下 ...

  10. CppCMS1.0.3 Build by VS2012

    1.CppCMS简介 CppCMS是一个C++的Web开发框架(不是一个CMS).它不同于大多数其他Web开发框架,如巨蟒Django , Java的Servlets ,或C++ Wt因为它在设计和调 ...