通过Python注释和取消注释XML
我想知道如何用Python在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>
我希望它能变成这样:
<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>
然后根据需要再去掉注释……或者
我正在使用xml.dom中的minidom。请问我需要使用其他的XML解析器吗?我更希望能避免使用正则表达式……那样会很麻烦。
3 个回答
-1
使用ElementTree:
from xml.etree import ElementTree as etree
import sys
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>
"""
doc = etree.fromstring(xml)
antfiles = doc.getiterator("ant")
antfiles[1].tag = "!--" # Comment the second antfile
print etree.tostring(doc)
# >>>
# <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" />
# <!-- antfile="build.xml" dir="Folio/Folio" inheritall="false" target="package" />
# </target>
###
2
在编程中,有时候我们会遇到一些问题,比如代码运行不正常或者出现错误。这种情况下,我们需要找到问题的根源。通常,我们可以通过查看错误信息来帮助我们理解发生了什么。
错误信息就像是程序在告诉我们:“嘿,我遇到了一些麻烦!”这些信息通常会包含出错的行号和错误的类型。了解这些信息可以帮助我们更快地找到并解决问题。
此外,很多时候我们可以在网上找到类似的问题和解决方案,比如在StackOverflow这样的社区。这里有很多经验丰富的程序员分享他们的知识和经验,帮助我们解决问题。
总之,遇到问题时,不要慌张,仔细查看错误信息,利用网络资源,通常都能找到解决办法。
a='''<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>
'''
import xml.etree.ElementTree as ET
from xml.etree.ElementTree import Comment, tostring
root = ET.fromstring(a)
element = root.getchildren()[2]
comment_element = Comment(tostring(element))
root.insert(2, comment_element)
root.remove(element)
print tostring(root)
5
下面的脚本使用了 xml.dom.minidom
这个库,并且包含了可以对节点进行注释和取消注释的功能:
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>