转载:

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()
print

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>
 

最新文章

  1. Java的流程控制和C++的异同
  2. SQl server 关于重复插入数据的测试
  3. ASP.NET MVC3入门教程之ajax交互
  4. [转载] Linux下高并发socket最大连接数所受的各种限制
  5. find a filename from a filehandle in Perl
  6. Manually connecting to the Oracle Linux Yum Server
  7. 【UVALive - 3713】Astronauts (2-SAT)
  8. kvm虚拟化之克隆篇
  9. [基础架构]PeopleSoft Application Server 重要文件说明
  10. MySQL表添加自增列
  11. kubernetes in action - Volumes
  12. 单片机成长之路(51基础篇) - 021 STC89C51系列单片机 内部EEPROM 驱动
  13. linux下postgresql的连接数配置
  14. redmine和jenkins的ldap登录设置
  15. 如何让win2008服务器显示中文无乱码
  16. 开启Node.js的大门
  17. 使用Pypi镜像源加速第三方库的安装
  18. mysql 修改密码的几种方式
  19. MySQL函数库
  20. MSER(Maximally Stable Extremal Regions)算法总结

热门文章

  1. linux下对/sys/class/gpio中的gpio的控制 (转)
  2. 在有道词典程序文件夹发现一个后缀名为sql的数据库(SQLite)
  3. IE6对!important单个的类是支持的
  4. Codeforces Round #281 (Div. 2) B 模拟
  5. 怎么用SecureCRT这个工具把linux服务器的压缩文件下载到本地的一个路径。
  6. java 复习整理(一 java简介和基础语法)
  7. 无线网络发射器选址 (NOIP2014)(真&#183;纯模拟)
  8. STM in Haskell
  9. Xcode_Build_Setting_Reference
  10. Linux中断(interrupt)子系统之五:软件中断(softIRQ)【转】