Python script to remove all comments from XML file

Posted by Jennifer Greentree on Stack Overflow See other posts from Stack Overflow or by Jennifer Greentree
Published on 2012-05-03T17:47:20Z Indexed on 2013/11/12 21:54 UTC
Read the original article Hit count: 222

Filed under:
|
|
|

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.

© Stack Overflow or respective owner

Related posts about python

Related posts about Xml