2012-01-06 68 views
3

我想知道如何使用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> 

,然后再删除评论需要... 或

我使用minidom命名从xml.dom的。我需要使用不同的XML解析器吗?宁愿避免使用正则表达式...这将是一场噩梦。

+0

应该怎样一个指定要c的内容omment(行号,x + y位置)? – 2012-01-06 21:31:37

+0

希望通过属性,如果其可能 – 2012-01-06 21:44:49

回答

4

下面的脚本使用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> 
+0

这是否适合现有的评论?假设我们将其序列化为一个文件。回来打开文件解析它,然后删除评论...这个例子工作,它似乎仍然需要树仍然存在... – 2012-01-09 14:19:15

+0

@MichaelBallent。是的,如果xml被重新解析,它会起作用。我已经更新了示例脚本和输出以澄清这一点。 – ekhumoro 2012-01-09 17:58:08

+0

接受这个答案,因为它让我继续使用minidom。非常感谢你。 – 2012-01-09 19:38:46

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

### 
+0

标记评论结束的' - >'位缺失。 – mzjn 2012-01-06 22:07:55

+0

@mzjn:你是对的,那么我会像用户一样使用et.Comment ...在其他答案中做过。 – PabloG 2012-01-06 22:16:27

相关问题