How to prevent xml.ElementTree fromstring from dropdown commentnode

I have the following code snippet:

from xml.etree.ElementTree import fromstring,tostring mathml = fromstring(input) for elem in mathml.getiterator(): elem.tag = 'm:' + elem.tag return tostring(mathml) 

When I enter the following input :

 <math> <a> 1 2 3 </a> <b /> <foo>Uitleg</foo> <!-- <bar> --> </math> 

This leads to:

 <m:math> <m:a> 1 2 3 </m:a> <m:b /> <m:foo>Uitleg</m:foo> </m:math> 

How did it happen? And how can I save a comment?

edit : I don't need the exact xml library, however I should be able to embed changes in tags. Unfortunately, lxml does not seem to allow this (and I cannot use the correct namespace operations)

+10
source share
1 answer

You cannot with xml.etree because its parser ignores comments (which, by the way, is acceptable behavior for an XML parser). But you can, if you use a (compatible) lxml library that allows you to configure parser options .

 from lxml import etree parser = etree.XMLParser(remove_comments=False) tree = etree.parse('input.xml', parser=parser) # or alternatively set the parser as default: # etree.set_default_parser(parser) 

This would be the easiest option. If you really need to use xml.etree, you can try connecting your own parser, although even then comments are not officially supported: look at this example (from the author xml.etree) (still seems to work in python 2.7 by the way)

+14
source

All Articles