I would like to comment out a text like this:
<name>cranberry</name>
However, my script returns the output like this:
<!-- <name>cranberry</name> -->
My Script:
import xml.etree.ElementTree as ET
from xml.etree.ElementTree import Comment
tree = ET.parse(r"C:\sample.xml")
root = tree.getroot()
comment = ET.Comment("<name>cranberry</name>")
root.insert(0,comment)
tree.write(r"C:\sample1.xml")
Any advice would be appreciated.
The older ElementTree library included in Python 2.6 does indeed XML-escape data in comments unconditionally:
You have a few options:
Upgrade to Python 2.7; it handles comment serialization correctly:
Install the external ElementTree library.
Use the Minidom (not recommended, the DOM API is overly verbose):