Reputation: 1305
I'd like to preserve comments as faithfully as possible while manipulating XML.
I managed to preserve comments, but the contents are getting XML-escaped.
#!/usr/bin/env python
# add_host_to_tomcat.py
import xml.etree.ElementTree as ET
from CommentedTreeBuilder import CommentedTreeBuilder
parser = CommentedTreeBuilder()
if __name__ == '__main__':
filename = "/opt/lucee/tomcat/conf/server.xml"
# this is the important part: use the comment-preserving parser
tree = ET.parse(filename, parser)
# get the node to add a child to
engine_node = tree.find("./Service/Engine")
# add a node: Engine.Host
host_node = ET.SubElement(
engine_node,
"Host",
name="local.mysite.com",
appBase="webapps"
)
# add a child to new node: Engine.Host.Context
ET.SubElement(
host_node,
'Context',
path="",
docBase="/path/to/doc/base"
)
tree.write('out.xml')
#!/usr/bin/env python
# CommentedTreeBuilder.py
from xml.etree import ElementTree
class CommentedTreeBuilder ( ElementTree.XMLTreeBuilder ):
def __init__ ( self, html = 0, target = None ):
ElementTree.XMLTreeBuilder.__init__( self, html, target )
self._parser.CommentHandler = self.handle_comment
def handle_comment ( self, data ):
self._target.start( ElementTree.Comment, {} )
self._target.data( data )
self._target.end( ElementTree.Comment )
However, comments like like:
<!--
EXAMPLE HOST ENTRY:
<Host name="lucee.org" appBase="webapps">
<Context path="" docBase="/var/sites/getrailo.org" />
<Alias>www.lucee.org</Alias>
<Alias>my.lucee.org</Alias>
</Host>
HOST ENTRY TEMPLATE:
<Host name="[ENTER DOMAIN NAME]" appBase="webapps">
<Context path="" docBase="[ENTER SYSTEM PATH]" />
<Alias>[ENTER DOMAIN ALIAS]</Alias>
</Host>
-->
End up as:
<!--
EXAMPLE HOST ENTRY:
<Host name="lucee.org" appBase="webapps">
<Context path="" docBase="/var/sites/getrailo.org" />
<Alias>www.lucee.org</Alias>
<Alias>my.lucee.org</Alias>
</Host>
HOST ENTRY TEMPLATE:
<Host name="[ENTER DOMAIN NAME]" appBase="webapps">
<Context path="" docBase="[ENTER SYSTEM PATH]" />
<Alias>[ENTER DOMAIN ALIAS]</Alias>
</Host>
-->
I also tried self._target.data( saxutils.unescape(data) )
in CommentedTreeBuilder.py
, but it didn't seem to do anything. In fact, I think the problem happens somewhere after the handle_commment()
step.
By the way, this question is similar to this.
Upvotes: 37
Views: 25185
Reputation: 301
The following worked for me out of the box (python 3.9):
from xml.etree.ElementTree import XMLParser, TreeBuilder, parse
ctb = TreeBuilder(insert_comments=True) # This does the trick :)
xp = XMLParser(target=ctb)
mytree = parse('sample.xml', parser=xp)
root = mytree.getroot()
mytree.write('sample_modified.xml')
Upvotes: 1
Reputation: 71
Martin's answer is correct, only it's missing some code, I understand that it may be obvious to more experienced programers, but as a new programer it took a minute for me to understand: Marting's answer:
import xml.etree.ElementTree as ET
from xml.etree import ElementTree
class CommentedTreeBuilder(ElementTree.TreeBuilder):
# This class will retain remarks and comments opposed to the xml parser default
def comment(self, data):
self.start(ElementTree.Comment, {})
self.data(data)
self.end(ElementTree.Comment)
# the missing part:
def parse_xml_with_remarks(filepath):
ctb = CommentedTreeBuilder()
xp = ET.XMLParser(target=ctb)
tree = ET.parse(filepath, parser=xp)
return tree
# parsing the file, and getting root
tree=parse_xml_with_remarks(file)
root=tree.getroot()
Upvotes: 5
Reputation: 6312
Tested with Python 2.7 and 3.5, the following code should work as intended.
#!/usr/bin/env python
# CommentedTreeBuilder.py
from xml.etree import ElementTree
class CommentedTreeBuilder(ElementTree.TreeBuilder):
def comment(self, data):
self.start(ElementTree.Comment, {})
self.data(data)
self.end(ElementTree.Comment)
Then, in the main code use
parser = ElementTree.XMLParser(target=CommentedTreeBuilder())
as the parser instead of the current one.
By the way, comments work correctly out of the box with lxml
. That is, you can just do
import lxml.etree as ET
tree = ET.parse(filename)
without needing any of the above.
Upvotes: 48
Reputation: 10390
Python 3.8 added the insert_comments
argument to TreeBuilder
which:
class xml.etree.ElementTree.TreeBuilder(element_factory=None, *, comment_factory=None, pi_factory=None, insert_comments=False, insert_pis=False)
When insert_comments and/or insert_pis is true, comments/pis will be inserted into the tree if they appear within the root element (but not outside of it).
Example:
parser = ElementTree.XMLParser(target=ElementTree.TreeBuilder(insert_comments=True))
Upvotes: 20
Reputation: 4484
Looks like both answers from @Martin and @sukhbinder didn't work for me... So made this as a workable completed solution on python 3.x
from xml.etree import ElementTree
string = '''<?xml version="1.0"?>
<data>
<!--Test
-->
<country name="Liechtenstein">
<rank>1</rank>
<year>2008</year>
<gdppc>141100</gdppc>
<neighbor name="Austria" direction="E"/>
<neighbor name="Switzerland" direction="W"/>
</country>
</data>'''
class CommentedTreeBuilder(ElementTree.TreeBuilder):
def comment(self, data):
self.start(ElementTree.Comment, {})
self.data(data)
self.end(ElementTree.Comment)
parser = ElementTree.XMLParser(target=CommentedTreeBuilder())
tree = ElementTree.fromstring(string, parser)
print(tree.find("./*[0]").text)
# or ElementTree.parse(filename, parser)
Upvotes: 3
Reputation: 1121
Martin's Code didn't work for me. I modified the same with the following which works as intended.
import xml.etree.ElementTree as ET
class CommentedTreeBuilder(ET.XMLTreeBuilder):
def __init__(self, *args, **kwargs):
super(CommentedTreeBuilder, self).__init__(*args, **kwargs)
self._parser.CommentHandler = self.comment
def comment(self, data):
self._target.start(ET.Comment, {})
self._target.data(data)
self._target.end(ET.Comment)
This is the test
parser=CommentedTreeBuilder()
tree = ET.parse(filename, parser)
tree.write('out.xml')
Upvotes: 5