注释和通过Python取消注释XML [英] Commenting and uncommenting XML via Python
本文介绍了注释和通过Python取消注释XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道的方式发表意见,并使用Python的XML注释的元素。
I would like to know of a way to comment and uncomment an element in XML using Python.
<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>
我
怎样才能得到它看起来像这样:
How can I get it to look like this:
<target depends="create-build-dir" name="build-Folio">
<property name="project.name" value="Folio"/>
<ant antfile="build.xml" dir="Folio/FolioUI" inheritall="false" target="deploy"/>
<!-- <ant antfile="build.xml" dir="Folio/Folio" inheritall="false" target="deploy"/> -->
</target>
根据需要
,然后再删除评论...
或
and then remove the comments again as needed... or
我使用从xml.dom的minidom命名。我需要使用不同的XML解析器?将preFER避免使用正则表达式...这将是一场噩梦。
I am using minidom from xml.dom. Do I need to use a different XML parser? Would prefer to avoid using regex... that would be a nightmare.
推荐答案
下面使用脚本 xml.dom.minidom
键,功能包括两个注释和取消注释节点:
The script below uses xml.dom.minidom
and includes functions for both commenting and uncommenting nodes:
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
输出:
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>
这篇关于注释和通过Python取消注释XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文