Python中的XML注释和取消注释

2024-09-30 22:14:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我想知道一种使用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>

我怎样才能让它看起来像这样:

^{pr2}$

然后根据需要再次删除评论。。。 或者

我正在使用minidomxml.dom文件. 我需要使用不同的XML解析器吗?希望避免使用正则表达式。。。那将是一场噩梦。在


Tags: 方法namebuildfalse元素packagetargetdir
3条回答

{and uncommenting下面的函数}包括对以下节点的注释:

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

输出:

^{pr2}$

使用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>

###
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)

相关问题 更多 >