Title: xml.etree.ElementTreee.write can't parse its own output
Type: behavior Stage: resolved
Components: XML Versions: Python 2.7
Status: closed Resolution: duplicate
Dependencies: Superseder: ElementTree and minidom don't prevent creation of not well-formed XML
View: 5166
Assigned To: Nosy List: François-Michel L'Heureux, ned.deily, r.david.murray
Priority: normal Keywords:

Created on 2015-03-12 17:32 by François-Michel L'Heureux, last changed 2015-03-12 19:48 by ned.deily. This issue is now closed.

File name Uploaded Description Edit François-Michel L'Heureux, 2015-03-12 19:07 Issue example
Messages (5)
msg237965 - (view) Author: François-Michel L'Heureux (François-Michel L'Heureux) Date: 2015-03-12 17:32
Using TreeBuilder to put data into XML tree.
Convert that tree to a string.
Parse that string.
XML parser error.

I expect XML library to be able to parse its own output.

Reference example:
msg237966 - (view) Author: François-Michel L'Heureux (François-Michel L'Heureux) Date: 2015-03-12 17:36
Note that you cannot copy paste the code and expect to reproduce the issue because it contains special characters that cannot be copy pasted via a web browser.

Here is the output when run.

<?xml version='1.0' encoding='utf-8'?>
<top><line>2015-03-12 09:44:54.560 script runner plugin Hello, world</line></top>
Traceback (most recent call last):
  File "", line 17, in <module>
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/etree/", line 1300, in XML
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/etree/", line 1642, in feed
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/etree/", line 1506, in _raiseerror
    raise err
xml.etree.ElementTree.ParseError: not well-formed (invalid token): line 2, column 35
msg237969 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2015-03-12 17:56
Use \x escapes to construct your example, then.
msg237977 - (view) Author: François-Michel L'Heureux (François-Michel L'Heureux) Date: 2015-03-12 19:07
Updated file where copy/pasting works.
msg237979 - (view) Author: Ned Deily (ned.deily) * (Python committer) Date: 2015-03-12 19:48
This is a duplicate of Issue5166.  Note that in your example, you are not serializing valid XML 1.0: \x1b (ESC) is not a valid XML character, it has to be escaped and, as the discussion in Issue5166 points out, etree does not automatically do that for you.
Date User Action Args
2015-03-12 19:48:58ned.deilysetstatus: open -> closed

superseder: ElementTree and minidom don't prevent creation of not well-formed XML

nosy: + ned.deily
messages: + msg237979
resolution: duplicate
stage: resolved
2015-03-12 19:07:27François-Michel L'Heureuxsetfiles: +

messages: + msg237977
2015-03-12 19:06:46François-Michel L'Heureuxsetfiles: -
2015-03-12 17:56:22r.david.murraysetnosy: + r.david.murray
messages: + msg237969
2015-03-12 17:36:59François-Michel L'Heureuxsetmessages: + msg237966
2015-03-12 17:32:38François-Michel L'Heureuxsettype: behavior
2015-03-12 17:32:05François-Michel L'Heureuxcreate