Python ElementTree "no element found" exception
Asked Answered
D

3

11

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!

Davin answered 11/4, 2014 at 1:2 Comment(0)
G
15

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

Globulin answered 27/5, 2014 at 20:32 Comment(0)
D
7

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/>
Dissonancy answered 11/4, 2014 at 1:5 Comment(4)
What do you mean by "format"? How should I do it better?Davin
@user3424423 I've just indented the xml so it is easier to see that the xml is not correctly structured. Elementtree cannot handle it.Dissonancy
This does not help me at all. What do I have to do better?Davin
To clarify the above old answer for anyone stumbling upon it now: The 'close' tags are wrong <tag/> instead of </tag> - This creates a NEW tag, nested under the one that was expected to be closed, rather than closing it. Moving the / to the front of the tag name will correct part of the issue, plus adding close tags for the other entries.Meritorious
Q
0

I face similar issues in the past but if you clearly see all the open tags in the messy file and its counter closing tags with </> syntax, it is usually the main issue with xml files.

Q answered 16/5, 2022 at 13:39 Comment(1)
This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. To get notified when this question gets new answers, you can follow this question. Once you have enough reputation, you can also add a bounty to draw more attention to this question. - From ReviewAroid

© 2022 - 2024 — McMap. All rights reserved.