0

I am trying to use the Element Tree modules but I end up to some Error which I can't understand.

My code here is based on the Python documentation itself, Python Element Tree doc ,somehow it gave me an error when trying to run the script;

try: import xml.etree.cElementTree as ET except ImportError: import xml.etree.ElementTree as ET file_name_xml = "curl-result.xml" tree = ET.parse(file_name_xml) tree.getroot() 

When I run this code:

./python2.6 modify_xml_file.py 

Then, it gave me this error;

Traceback (most recent call last): File "modify_xml_file.py", line 8, in <module> tree = ET.parse(file_name_xml) File "<string>", line 45, in parse File "<string>", line 32, in parse SyntaxError: not well-formed (invalid token): line 1, column 4 
0

1 Answer 1

2

The version of cElementTree included in Python 2.6 throws a SyntaxError exception for malformed XML:

>>> with open('bad.xml', 'w') as badxml: ... badxml = '<foobar\n' ... >>> import xml.etree.cElementTree as ET >>> tree = ET.parse('bad.xml') Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<string>", line 45, in parse File "<string>", line 32, in parse SyntaxError: no element found: line 1, column 0 

This is a bug in the C acceleration code fixed in Python 2.7. The (slower) Python parser throws a more helpful error:

>>> import xml.etree.ElementTree as ET >>> tree = ET.parse('bad.xml') Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/Users/mjpieters/Development/Library/buildout.python/parts/opt/lib/python2.6/xml/etree/ElementTree.py", line 862, in parse tree.parse(source, parser) File "/Users/mjpieters/Development/Library/buildout.python/parts/opt/lib/python2.6/xml/etree/ElementTree.py", line 587, in parse self._root = parser.close() File "/Users/mjpieters/Development/Library/buildout.python/parts/opt/lib/python2.6/xml/etree/ElementTree.py", line 1254, in close self._parser.Parse("", 1) # end of data xml.parsers.expat.ExpatError: no element found: line 1, column 0 

Fix your XML input file.

What changed in 2.7 is that ElementTree was updated to version 1.3, a version that improved the parser, introducing a new ParseError exception, which is a subclass of SyntaxError.

Sign up to request clarification or add additional context in comments.

5 Comments

After removing additional data of the curl result on the top of my xml, I managed to run the code. Thank you, but of course, I was very sure the code is not malformed before because I tried to read it online using xmlgrid.net . Anyway, thank you. I don't know why this question should get downvoted though. I tried to ask a question which I have done some research before, maybe my fault for asking inappropriate question. Thank you again. :)
@yunaranyancat: try to make your question as complete as possible; including your XML (and how you produced it) would have made it much easier for people to answer, because we can then verify the issue. This is called a minimal reproducible example. Creating a proper MCVE (where you test the steps to see if the problem is indeed still there) can often already lead to a solution.
Noted with thanks, will use this guide in the future.
@yunaranyancat: I'm not saying that that is why it was downvoted (we can't read the minds, yet, unfortunately), but that could be a reason.
It's okay, at least you gave me the good ideas so I can relate and improve with my way of asking questions..

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.