I'm using python version 2.7.3.
test.xml:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<test>The tag "StackOverflow" is good to bring up at parties.</test>
</root>
I will change the text in the xml with elementtree. Then I write to the file.
But the the <
is changed to <
etc. It will be:
test.xml:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<test>The tag "StackOverflow" is good to bring up at parties.</test>
</root>
When I write to the file with elementtree.write What I'd like to see:
test.xml:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<test>The tag "StackOverflow" is good to bring up at parties.</test>
</root>
I'd like to use this text as-is for display within XML, therefore I don't want an XML parser to mess with it. Is there any way?
I use this code
import xml.etree.ElementTree as ET
class CommentedTreeBuilder(ET.XMLTreeBuilder):
def __init__(self):
ET.XMLTreeBuilder.__init__(self)
self._parser.CommentHandler = self.handle_comment
def handle_comment(self, data):
self._target.start(ET.Comment, {})
self._target.data(data)
self._target.end(ET.Comment)
with open('test.xml', 'r') as f:
tree = ET.parse(f, parser=CommentedTreeBuilder())
root = tree.getroot()
test = root.find('test')
test.text += 'test'
tree.write('test.xml', encoding="UTF-8", xml_declaration=True)
>
doesnt have the problem. "


have the problem