Title: ElementTree won't parse comments before root element
Type: enhancement Stage: resolved
Components: XML Versions: Python 3.6
Status: closed Resolution: duplicate
Dependencies: Superseder: xml.etree.ElementTree skips processing instructions when parsing
View: 9521
Assigned To: Nosy List: amaury.forgeotdarc, celinecyc, effbot, flox, poke, scoder
Priority: normal Keywords:

Created on 2019-11-08 08:10 by celinecyc, last changed 2019-11-15 17:41 by scoder. This issue is now closed.

Messages (2)
msg356229 - (view) Author: celinecyc (celinecyc) Date: 2019-11-08 08:10
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". 


<?xml version="1.0" encoding="utf-8"?>
<!-- comments here doesn't work -->
<root node>
  <nodeA />
  <!-- comments here is ok -->
  <nodeB />
</root node>
from xml.etree import ElementTree
class MyTreeBuilder(ElementTree.TreeBuilder):
   def comment(self, data):
       self.start(ElementTree.Comment, {})
with open('c:/temp/t.xml', 'r') as f:
   xml = ElementTree.parse(
       f, parser=ElementTree.XMLParser(target=MyTreeBuilder()))
msg356698 - (view) Author: Stefan Behnel (scoder) * (Python committer) Date: 2019-11-15 17:41
Duplicate of issue 9521 (and issue 24287).
Date User Action Args
2019-11-15 17:41:44scodersetstatus: open -> closed
superseder: xml.etree.ElementTree skips processing instructions when parsing
messages: + msg356698

resolution: duplicate
stage: resolved
2019-11-08 08:10:53celinecyccreate