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. 刷题总结——Throw nails(hdu4393)

    题目: Problem Description The annual school bicycle contest started. ZL is a student in this school. H ...

  2. ROS内usb_cam包使用注意事项

    1.查看摄像头支持的pixel-format: 方法:  v4l2-ctl --list-formats-ext -d /dev/video0

  3. libcurl网络连接使用tcp/ip

    CURL *curl; CURLcode res; const char *request = "GETas.xxxxE测试发送"; curl_socket_t sockfd; / ...

  4. FOJ ——Problem 1759 Super A^B mod C

     Problem 1759 Super A^B mod C Accept: 1368    Submit: 4639Time Limit: 1000 mSec    Memory Limit : 32 ...

  5. 在线cron生成器

    什么是cron表达式: cron表达式主要是用在Quartz框架中,Quartz是一个完全由java编写的开源作业调度框架,最主要的功能就是调度器(完成定时任务),可以与javaEE或者javaSE应 ...

  6. java算法(一) 直接选择排序

    一.基本概念 首先在未排序序列中找到最小(大)元素,存放到排序序列的起始位置(注:要把最大最小的元素和起始的元素交换), 然后,再从剩余未排序元素中继续寻找最小(大)元素,然后放到已排序序列的末尾.以 ...

  7. hdu6188&&百度之星初赛(B) T5

    度度熊的交易计划 Problem Description 度度熊参与了喵哈哈村的商业大会,但是这次商业大会遇到了一个难题: 喵哈哈村以及周围的村庄可以看做是一共由n个片区,m条公路组成的地区. 由于生 ...

  8. .h 和 .cpp 切换快捷键设置(转)

    原文转自 https://blog.csdn.net/t163361/article/details/51859274 打开VS后依次选择如下 工具-> 选项-> 环境-> 键盘-& ...

  9. windows技术

    鼠标右键菜单中没有新建文本文件怎么办? http://jingyan.baidu.com/article/046a7b3e1d737bf9c27fa9f7.html

  10. Java解决跨域的方案

    在后台加上,在数据返回之前添加 response.setHeader("Access-Control-Allow-Origin","*"); 就可以了,前台不用 ...