Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过Python注释和取消注释XML_Python_Xml_Ant_Xml Parsing_Minidom - Fatal编程技术网

通过Python注释和取消注释XML

通过Python注释和取消注释XML,python,xml,ant,xml-parsing,minidom,Python,Xml,Ant,Xml Parsing,Minidom,我想知道一种使用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

我想知道一种使用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解析器吗?希望避免使用正则表达式。。。那将是一场噩梦。

a=''
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)
''' 将xml.etree.ElementTree作为ET导入 从xml.etree.ElementTree导入注释到字符串 root=ET.fromstring(a) 元素=root.getchildren()[2] 注释\元素=注释(tostring(元素)) root.insert(2,comment\u元素) root.remove(元素) 打印到字符串(根)
带元素树:

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>

###
从xml.etree导入元素树作为etree
导入系统
xml=”“”
"""
doc=etree.fromstring(xml)
antfiles=doc.getiterator(“ant”)
antfiles[1]。标记=“!-->”注释第二个antfiles
打印etree.tostring(文档)
# >>>
# 
#    
#    

#下面的脚本使用
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
从xml.dom导入minidom
xml=”“”\
"""
def注释_节点(节点):
comment=node.ownerDocument.createComment(node.toxml())
node.parentNode.replaceChild(注释,节点)
回复评论
def取消注释节点(注释):
node=minidom.parseString(comment.data).firstChild
comment.parentNode.replaceChild(节点,注释)
返回节点
doc=minidom.parseString(xml).documentElement
注释节点(doc.getElementsByTagName('ant')[-1])
xml=doc.toxml()
打印“注释节点():\n”
打印xml
打印
doc=minidom.parseString(xml).documentElement
comment=doc.lastChild.previousSibling
打印“重新分析的注释:\n”
打印comment.toxml()
打印
取消注释节点(注释)
打印“取消注释节点():\n”
打印doc.toxml()
打印
输出:

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>
comment_node():
重新分析的注释:
取消注释节点():

应该如何指定要注释的内容(行号,x+y位置)?如果是possible@mzjn:你是对的,那么我会使用et。像用户一样评论。。。在另一个答案中有。这对现有的评论有效吗?假设我们将其序列化为一个文件。回来打开文件解析它然后删除注释。。。如果这个例子有效,它似乎仍然需要树仍然存在…@MichaelBallent。是的,如果xml被重新解析,它就会工作。我已经更新了我的示例脚本和输出,以澄清这一点。接受这个答案,因为它允许我继续使用minidom。非常感谢。如果我的评论是
而不是
@Ridhuvarshan怎么办。这毫无意义。如果取消注释,将导致无效的xml。这个问题是关于注释和取消注释xml标记/节点,而不是任意字符串。