Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python ElementTree "no element found" exception

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!

like image 895
Nearoo Avatar asked Apr 11 '14 01:04

Nearoo


2 Answers

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

like image 81
grvsmth Avatar answered Nov 20 '22 14:11

grvsmth


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/>
like image 26
alecxe Avatar answered Nov 20 '22 13:11

alecxe