[New-bugs-announce] [issue38742] ElementTree won't parse comments before root element

yucheng chen report at bugs.python.org
Fri Nov 8 03:10:53 EST 2019


New submission from yucheng chen <celinecyc1996 at gmail.com>:

issue8277
It couldn't work for those comments before the root element. 
It will raise an error that "xml.etree.ElementTree.ParseError: multiple elements on top level". 

Example:

test.xml
--------
<?xml version="1.0" encoding="utf-8"?>
<!-- comments here doesn't work -->
<root node>
  <nodeA />
  <!-- comments here is ok -->
  <nodeB />
</root node>

test.py
-------
from xml.etree import ElementTree
class MyTreeBuilder(ElementTree.TreeBuilder):
   def comment(self, data):
       self.start(ElementTree.Comment, {})
       self.data(data)
       self.end(ElementTree.Comment)
with open('c:/temp/t.xml', 'r') as f:
   xml = ElementTree.parse(
       f, parser=ElementTree.XMLParser(target=MyTreeBuilder()))
ElementTree.dump(xml)

----------
components: XML
messages: 356229
nosy: amaury.forgeotdarc, effbot, flox, poke, scoder, yucheng chen
priority: normal
severity: normal
status: open
title: ElementTree won't parse comments before root element
type: enhancement
versions: Python 3.6

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue38742>
_______________________________________


More information about the New-bugs-announce mailing list