Python XML: ParseError: junk after document element

As @Matthias Wiehl said, ElementTree expects only a single root node and is not well-formed XML, which should be fixed at its origin. As a workaround you can add a fake root node to the document.

import xml.etree.cElementTree as ET
import re

with open("index.xml") as f:
    xml = f.read()
tree = ET.fromstring(re.sub(r"(<\?xml[^>]+\?>)", r"\1<root>", xml) + "</root>")

The root node of your document (Version) is opened and closed on line 2. The parser does not expect any nodes after the root node. Solution is to remove the closing forward slash.

Tags:

Python

Xml