I am trying to build a python script that will take in an XML document and remove all of the comment blocks from it.
I tried something along the lines of:
tree = ElementTree()
tree.parse(file)
commentElements = tree.findall('//comment()')
for element in commentElements:
element.parentNode.remove(element)
Doing this yields a weird error from python: "KeyError: '()'
I know there are ways to easily edit the file using other methods ( like sed ), but I have to do it in a python script.
解决方案
comment() is an XPath node test that is not supported by ElementTree.
You can use comment() with
Here is how you can remove comments with lxml:
from lxml import etree
XML = """
TEXT 1
TEXT 2
"""
tree = etree.fromstring(XML)
comments = tree.xpath('//comment()')
for c in comments:
p = c.getparent()
p.remove(c)
print etree.tostring(tree)
Output:
TEXT 1
TEXT 2