Python ElementTree "no element found" exception

32,945

Solution 1

Your tags are not closed properly. For example, to close a "meteorite" tag, use </meteorite> not <meteorite />.

Solution 2

You XML is not well-formed, ElementTree cannot parse it - it really looks like it is a part of a real document.

Here's what you get if you format it:

<level>
    <leveldata>
        <level name="hh" difficulty="Easy" lenght="3600">
            <meteorite chance="4" speed="3">
                <image id="1">
                    <image id="2">
                        <image id="3">
                            <meteorite/>
                            <meteorite chance="4" speed="3">
                                <image id="4">
                                    <image id="5">
                                        <image id="6">
                                            <meteorite/>
                                            <level/>
                                            <leveldata/>
                                            <meteorimages>
                                                <meteor id="5" imagepath="res\meteorit_1.png">
                                                    <meteor id="5" imagepath="res\meteorit_2.png">
                                                        <meteor id="5" imagepath="res\meteorit_3.png">
                                                            <meteorimages/>
                                                            <datasheet/>
                                                            <level/>
Share:
32,945
Nearoo
Author by

Nearoo

CS Student @ ETH Zürich

Updated on July 09, 2022

Comments

  • Nearoo
    Nearoo almost 2 years

    Good day everybody.

    I'm trying to read, parse and use an xml-file using ElementTree. Following data:

    <level>
        <leveldata>
            <level name="hh" difficulty="Easy" lenght="3600">
                <meteorite chance="4" speed="3" >
                    <image id="1">
                    <image id="2">
                    <image id="3">
                <meteorite />
                <meteorite chance="4" speed="3" >
                    <image id="4">
                    <image id="5">
                    <image id="6">
                <meteorite />
            <level />
        <leveldata />
        <meteorimages>
            <meteor id="5" imagepath="res\meteorit_1.png">
            <meteor id="5" imagepath="res\meteorit_2.png">
            <meteor id="5" imagepath="res\meteorit_3.png">
        <meteorimages />
    <datasheet />
    <level />
    

    Sadly, I ElementTree gives an exception!!! Reading the file with following code:

    import xml.etree.ElementTree as ET
    ***code***
    tree = ET.parse("res\\data.xml")
    root = tree.getroot()
    

    Exception:

    File "E:\blabla\core.py", line 26, in load_levelproperties
        *tree = ET.parse("res\\data.xml")*   File "E:\Programme(x86)\Python2.7x86\lib\xml\etree\ElementTree.py", line
    1182, in parse
        *tree.parse(source, parser)*   File "E:\Programme(x86)\Python2.7x86\lib\xml\etree\ElementTree.py", line
    657, in parse
        *self._root = parser.close()*   File "E:\Programme(x86)\Python2.7x86\lib\xml\etree\ElementTree.py", line
    1654, in close
        *self._raiseerror(v)*   File "E:\Programme(x86)\Python2.7x86\lib\xml\etree\ElementTree.py", line
    1506, in _raiseerror
        ***raise err xml.etree.ElementTree.ParseError: no element found: line 16, column 9***
    

    I can't figure out what's wrong, I've tried to change data.xml in every possible way I can imagine, no difference. It's always the last line of the file! What am I doing wrong? Thanks!