Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python - xml.etree.ElementTree.ParseError: not well-formed (invalid token)

I have the following code:

import xml.etree.ElementTree as ETree

parser = ETree.XMLParser(encoding="utf-8")
tree = ETree.fromstring("C:/Users/XXX/Downloads/test_xml.xml", parser=parser)
print(ETree.tostring(tree))

I get the following error message:

Traceback (most recent call last):
  File "C:/Users/XXX/.PyCharmCE2018.1/config/scratches/scratch.py", line 6, in <module>
    tree = ETree.fromstring("C:/Users/XXX/Downloads/test_xml.xml", parser=parser)
  File "C:\Users\XXX\AppData\Local\Programs\Python\Python36-32\lib\xml\etree\ElementTree.py", line 1314, in XML
    parser.feed(text)
xml.etree.ElementTree.ParseError: not well-formed (invalid token): line 1, column 2

I checked probably all questions to this error message on StackOverflow, nothing helped:

  • I tried to edit file with another editor (as adviced here);
  • I added this line: tree.set('SignalStrength',"100") (from here);
  • Tried to add DOCTYPE;
  • Checked the file with W3 Validator;

etc.

Then I tried to import another XML file with completely another structure - and error message remained the same - even the position: line 1, column 2.

And then I tried to change the file's name to the non-existent - and the error message remained the same! So it is not a problem of file, it is something else. And I can't understand what.

P.S. This is one of the XML files I used:

<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

EDIT: Probably I can't import file in the way I did in the fromstring() function?

like image 434
vlad.rad Avatar asked Oct 24 '25 12:10

vlad.rad


1 Answers

You need to use parse() instead of fromstring() when parsing from a file.

parse() returns an ElementTree instance and tostring() expects an Element instance.

This code works:

import xml.etree.ElementTree as ETree

parser = ETree.XMLParser(encoding="utf-8")
tree = ETree.parse("test_xml.xml", parser=parser)
print(ETree.tostring(tree.getroot()))
like image 140
mzjn Avatar answered Oct 27 '25 02:10

mzjn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!