Commenting and uncommenting XML via Python
2024-09-19 09:30:49
转载:
http://stackoverflow.com/questions/8764017/commenting-and-uncommenting-xml-via-python
from xml.dom import minidom xml = """\
<target depends="create-build-dir" name="build-Folio">
<property name="project.name" value="Folio"/>
<ant antfile="build.xml" dir="Folio/FolioUI" inheritall="false" target="package"/>
<ant antfile="build.xml" dir="Folio/Folio" inheritall="false" target="package"/>
</target>
""" def comment_node(node):
comment = node.ownerDocument.createComment(node.toxml())
node.parentNode.replaceChild(comment, node)
return comment def uncomment_node(comment):
node = minidom.parseString(comment.data).firstChild
comment.parentNode.replaceChild(node, comment)
return node doc = minidom.parseString(xml).documentElement comment_node(doc.getElementsByTagName('ant')[-1]) xml = doc.toxml() print 'comment_node():\n'
print xml
print doc = minidom.parseString(xml).documentElement comment = doc.lastChild.previousSibling print 're-parsed comment:\n'
print comment.toxml()
print uncomment_node(comment) print 'uncomment_node():\n'
print doc.toxml()
Output:
comment_node():<target depends="create-build-dir" name="build-Folio"><property name="project.name" value="Folio"/><ant antfile="build.xml" dir="Folio/FolioUI" inheritall="false" target="package"/><!--<ant antfile="build.xml" dir="Folio/Folio" inheritall="false" target="package"/>--></target>
re-parsed comment:<!--<ant antfile="build.xml" dir="Folio/Folio" inheritall="false" target="package"/>-->
uncomment_node():<target depends="create-build-dir" name="build-Folio"><property name="project.name" value="Folio"/><ant antfile="build.xml" dir="Folio/FolioUI" inheritall="false" target="package"/><ant antfile="build.xml" dir="Folio/Folio" inheritall="false" target="package"/></target>
最新文章
- Java的流程控制和C++的异同
- SQl server 关于重复插入数据的测试
- ASP.NET MVC3入门教程之ajax交互
- [转载] Linux下高并发socket最大连接数所受的各种限制
- find a filename from a filehandle in Perl
- Manually connecting to the Oracle Linux Yum Server
- 【UVALive - 3713】Astronauts (2-SAT)
- kvm虚拟化之克隆篇
- [基础架构]PeopleSoft Application Server 重要文件说明
- MySQL表添加自增列
- kubernetes in action - Volumes
- 单片机成长之路(51基础篇) - 021 STC89C51系列单片机 内部EEPROM 驱动
- linux下postgresql的连接数配置
- redmine和jenkins的ldap登录设置
- 如何让win2008服务器显示中文无乱码
- 开启Node.js的大门
- 使用Pypi镜像源加速第三方库的安装
- mysql 修改密码的几种方式
- MySQL函数库
- MSER(Maximally Stable Extremal Regions)算法总结
热门文章
- linux下对/sys/class/gpio中的gpio的控制 (转)
- 在有道词典程序文件夹发现一个后缀名为sql的数据库(SQLite)
- IE6对!important单个的类是支持的
- Codeforces Round #281 (Div. 2) B 模拟
- 怎么用SecureCRT这个工具把linux服务器的压缩文件下载到本地的一个路径。
- java 复习整理(一 java简介和基础语法)
- 无线网络发射器选址 (NOIP2014)(真&#183;纯模拟)
- STM in Haskell
- Xcode_Build_Setting_Reference
- Linux中断(interrupt)子系统之五:软件中断(softIRQ)【转】