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. UVA 11478(差分约束 + 二分)

    题意: 给定一个有向图,每条边都有一个权值,每次你可以选择一个结点和一个整数的,把所有以v为终点的边的权值减去d, 把所有以v为起点的边的权值加上d 最后要让所有边的权的最小值非负且尽量大 代码 #i ...

  2. hdu 1512

    思路:用并查集即可,每次合并的时候将小的集合合并到大的集合上去.理论上的平均复杂度是n*lgn*lgn. #include<map> #include<queue> #incl ...

  3. 基于RRT的机器人自主探索建图

    一.方法讲解: 本项目分为三个部分:机器人周围一定范围内基于RRT的全局检测, 根据上一步检测的未知区域点执行sklearn.cluster.MeanShift聚类,获取聚类中心: 根据聚类中心计算各 ...

  4. java的struts2整理

    readbook:struts2 先说一下struts1:    struts1使用model II 模式开发,即jsp+java bean+servlet    再说它的缺陷:    1.表现层支持 ...

  5. win8下notepad++无法设置文件关联

  6. How to duplicate a UIButton in Objective C?

    http://stackoverflow.com/questions/1092875/how-to-duplicate-a-uibutton-in-objective-c 1down vote To ...

  7. C 语言中char* 和const char*的区别

    const char *p = "123"; p[1] = '3'; // 会报错p = "456"; // 不会报错 const char * 只是说指针指向 ...

  8. linux系统查看主机序列号

    #dmidecode -t 1 System Information        Manufacturer: IBM        Product Name: System x3650 M3 -[7 ...

  9. aiohttp/asyncio测试代理是否可用

    #!/usr/bin/env python # encoding: utf-8 from proxyPool.db import RedisClient import asyncio import a ...

  10. 查看 Laravel 的 SQL 语句的方法

    在使用 Laravel 的 Eloquent 进行数据查询的时候,很多小伙伴都想看到背后执行的 SQL 语句到底是什么样的,这小笔录就是解决这个小问题的: 在 Providers/AppService ...