classification
Title: xml.etree.ElementTreee.write can't parse its own output
Type: behavior Stage: resolved
Components: XML Versions: Python 2.7
process
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.

Files
File name Uploaded Description Edit
python_xml_builder_bug.py 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: https://github.com/FinchPowers/python_xml_builder_bug/blob/d98b2422d9ecadbee37e2896a5098abf82b1d7a4/python_xml_builder_bug.py
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 "python_xml_builder_bug.py", line 17, in <module>
    ElementTree.fromstring(io.getvalue())
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/etree/ElementTree.py", line 1300, in XML
    parser.feed(text)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/etree/ElementTree.py", line 1642, in feed
    self._raiseerror(v)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/etree/ElementTree.py", 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.
History
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: + python_xml_builder_bug.py

messages: + msg237977
2015-03-12 19:06:46François-Michel L'Heureuxsetfiles: - python_xml_builder_bug.py
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