2012-01-06 13 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> 

:私はxml.domからminidomを使用しています または

...

<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パーサーを使用する必要がありますか?正規表現を使用することを避けることを好むだろう...それは悪夢になるだろう。 ElementTreeので

+0

どのようにすべき何をするかを指定する省略(行番号、x + y位置)? –

+0

もし可能であれば、属性によって可能であれば –

答えて

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

これは既存のコメントで機能しますか?これをファイルにシリアライズするとします。ファイルを開いて開き、コメントを削除します。この例はうまくいくでしょうか、ツリーがまだ存在する必要があるようです... –

+0

@MichaelBallent。はい、xmlが再解析された場合は動作します。私はそれを明確にするために私のサンプルスクリプトと出力を更新しました。 – ekhumoro

+0

minidomの使用を続けることができるので、この回答を受け入れてください。どうもありがとうございました。 –

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

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

+0

@mzjn:あなたが正しいです、そして私はet.Commentのようなユーザーを使用します...他の答えでそれをしました。 – PabloG

関連する問題