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. [codeforces] 25E Test || hash

    原题 给你三个字符串,找一个字符串(它的子串含有以上三个字符串),输出此字符串的长度. 先暴力判断是否有包含,消减需要匹配的串的数量.因为只有三个字符串,所以暴力枚举三个串的位置关系,对三个串跑好哈希 ...

  2. Spring----01. 入门知识,IoC/DI

    1.spring两个最基本概念:依赖注入DI.面向切面AOP 2.spring通过上下文Application Context装配bean,实现方式的区别是如何加载它们的配置信息, ClassPath ...

  3. Android JSON

    转自:http://www.open-open.com/lib/view/open1326376799874.html JSON的定义: 一 种轻量级的数据交换格式,具有良好的可读和便于快速编写的特性 ...

  4. 灰姑娘的水晶鞋(NOIP模拟赛Round 7)

    [问题描述] 传说中的水晶鞋有两种颜色:左边的水晶鞋是红色,右边的是蓝色,据说穿上它们会有神奇的力量. 灰姑娘要找到她所有的n双水晶鞋,它们散落在一条数轴的正半轴上,坐标各不相同,每双水晶鞋还有一个权 ...

  5. Xcode5 上64位编译 出错No architectures to compile for

    http://blog.csdn.net/chocolateloveme/article/details/16900999 详细错误信息如下: No architectures to compile ...

  6. Android SQLite使用

    1. 介绍 SQLite是一款轻型的数据库, 是遵守ACID的关系型数据库管理系统, Android系统已经在框架中适配接口供用户使用. 2. 数据类型 SQLite采用的是动态数据类型, 会根据存入 ...

  7. do_exit【转】

    转自:http://blog.csdn.net/lhf_tiger/article/details/8768874 进程在退出时,必须释放它所拥有的资源,并通过某种方式告诉父进程.进程的退出一般是显示 ...

  8. ubuntu16下安装telnet和opensshserver

    安装了虚拟机,使用的是ubuntu 16,server版本. 启动后发现没有telnet和ssh,就安装了(netstat -a|grep telnet). apt-get install openb ...

  9. 如何配置openjdk的 java home

    https://blog.csdn.net/redmoon729/article/details/51671354

  10. Linux/Unix面试题

    shell中如何改变文件中的某个关键字 unix命令 unix shell中在特定文件夹内查找包含指定字符串的文件用哪个命令 如何用要shell找到指定目录下的最近一天更新的文件,要包含子目录 Lin ...