转载:

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>
 

Commenting and uncommenting XML via Python的更多相关文章

  1. 如何将xml转为python中的字典

    如何将xml转为python中的字典 import cElementTree as ElementTree class XmlListConfig(list): def __init__(self, ...

  2. testlink用例转换小工具(excel转为xml,python版)

    前面文章记录了testlink的安装方法(CentOS 7下安装xampp和testlink),由于testlink仅支持xml格式的用例导入,研究了下excel转xml的方法, 从网上其他网友那里借 ...

  3. xml 解析 python

    1 综述 有很多种解析方法. (1)  DOM   缺点是:1 不能解析格式不正确或者不规则xml  2据说只能解析utf-8格式,非utf-8需要转码 与SAX比较,DOM典型的缺点是比较慢,消耗更 ...

  4. 【xml】python的lxml库使用

    1.官方教程:http://lxml.de/tutorial.html#parsing-from-strings-and-files  最重要的文档,看完基本就能用了 2.lxml支持xpath,xp ...

  5. 精通 Oracle+Python,第 6 部分:Python 支持 XML

    无可辩驳的是,XML 现在是软件中信息交换的实际标准. 因此,Oracle 数据库附带了各种与 XML 相关的增强和工具,它们统称为 Oracle XML DB.XML DB 包含一系列嵌入到数据库中 ...

  6. Python处理XML

    在Python(以及其他编程语言)内有两种常见的方法处理XML:SAX(Simple API for XML)和DOM(Document Object Model,文档对象模型).SAX语法分析器读取 ...

  7. [python标准库]XML模块

    1.什么是XML XML是可扩展标记语言(Extensible Markup Language)的缩写,其中的 标记(markup)是关键部分.您可以创建内容,然后使用限定标记标记它,从而使每个单词. ...

  8. python+selenium自动化软件测试(第12章):Python读写XML文档

    XML 即可扩展标记语言,它可以用来标记数据.定义数据类型,是一种允许用户对自己的标记语言进 行定义的源语言.xml 有如下特征: 首先,它是有标签对组成:<aa></aa> ...

  9. Python Web-第五周-Web Services and XML(Using Python to Access Web Data)

    1.Web Service Overview 1.Data on the Web Python Dictionary 和 Java HashMap间需要建立一个桥梁,可以用XML或是JSON 2.XM ...

随机推荐

  1. typedef函数用法

    转载自:http://www.cnblogs.com/ggjucheng/archive/2011/12/27/2303238.html 引言 typedef 声明,简称 typedef,为现有类型创 ...

  2. Codeforces Round #328 (Div. 2) C 数学

    C. The Big Race time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  3. DP———6.两个状态之间的 处理

    Tickets Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

  4. oracle修改数据遇到的坑

    select t.*,mt.*,mr.rowid,mr.* from manu_routecardlist mr left join manu_routecard t on t.routecard_i ...

  5. .ini配置读取

    ini文件的格式 格式 INI文件由节.键.值组成. 节 [section] 参数(键=值) name=value 注解 注解使用分号表示(;).在分号后面的文字,直到该行结尾都全部为注解.     ...

  6. Require.js 详细了解

    一.Require.js 作用 1.1.是JS 文件加载器,实现js脚本的AMD异步加载. 保证不阻塞页面的渲染和其后的脚本的执行,并提供了在加载完成之后的执行相应回调函数的功能. 1.2.实现JS. ...

  7. 1257 背包问题 V3——分数规划

    N个物品的体积为W1,W2......Wn(Wi为整数),与之相对应的价值为P1,P2......Pn(Pi为整数),从中选出K件物品(K <= N),使得单位体积的价值最大. Input 第1 ...

  8. Spring3.2+Struts2.3+Mybatis3.2整合使用(注解使用)

    0.包结构:

  9. 用vs2008和vs2005创建win32 console application

    http://blog.sina.com.cn/s/blog_4900be890100s735.html 对于经常使用vc6.0的人来说,创建一个win32 console application很简 ...

  10. Windows基础-实时录音程序(WaveXXX)

    写在前面 一开始是打算用这个老接口做讯飞语音识别的程序,在转移到UWP时发现,这玩意在Windows Runtime中屏蔽(弃用)了,将来会更新使用WASAPI的程序 WaveRecorder类代码下 ...