通过 Python 注释和取消注释 XML [英] Commenting and uncommenting XML via Python

查看:40
本文介绍了通过 Python 注释和取消注释 XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道一种使用 Python 对 XML 中的元素进行注释和取消注释的方法.

I would like to know of a way to comment and uncomment an element in XML using Python.

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

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

How can I get it to look like this:

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

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

and then remove the comments again as needed... or

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

I am using minidom from xml.dom. Do I need to use a different XML parser? Would prefer to avoid using regex... that would be a nightmare.

推荐答案

下面的脚本使用 xml.dom.minidom 并包括注释和取消注释节点的函数:

The script below uses xml.dom.minidom and includes functions for both commenting and uncommenting nodes:

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>

这篇关于通过 Python 注释和取消注释 XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆