Python ElementTree“未找到元素”异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23001757/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python ElementTree "no element found" exception
提问by Nearoo
Good day everybody.
大家好。
I'm trying to read, parse and use an xml-file using ElementTree. Following data:
我正在尝试使用 ElementTree 读取、解析和使用 xml 文件。以下数据:
<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:
可悲的是,我 ElementTree 给出了一个例外!!!使用以下代码读取文件:
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!
我不知道出了什么问题,我尝试以我能想象的所有可能方式更改 data.xml,没有区别。它始终是文件的最后一行!我究竟做错了什么?谢谢!
采纳答案by grvsmth
Your tags are not closed properly. For example, to close a "meteorite" tag, use </meteorite>
not <meteorite />
.
您的标签未正确关闭。例如,要关闭“meteorite”标签,请使用</meteorite>
not <meteorite />
。
回答by alecxe
You XML is not well-formed, ElementTree
cannot parse it - it really looks like it is a part of a real document.
您的 XML 格式不正确,ElementTree
无法解析它 - 它看起来真的像是真实文档的一部分。
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/>