尝试将XML文件解析为ElementTree:
>>> import xml.etree.cElementTree as ET
>>> tree = ET.ElementTree(file='D:\Temp\Slikvideo\JPEG\SV_4_1_mask\index.xml')
我收到以下错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Program Files\Anaconda2\lib\xml\etree\ElementTree.py", line 611, in __init__
self.parse(file)
File "<string>", line 38, in parse
ParseError: junk after document element: line 3, column 0
XML文件的开头如下:
<?xml version="1.0" encoding="UTF-8" ?>
<Version Writer="E:\d\src\Modules\SceneSerialization\src\mitkSceneIO.cpp" Revision="$Revision: 17055 $" FileVersion="1" />
<node UID="OBJECT_2016080819041580480127">
<source UID="OBJECT_2016080819041550469454" />
<data type="LabelSetImage" file="hfbaaa_Bolus.nrrd" />
<properties file="sicaaa" />
</node>
<node UID="OBJECT_2016080819041512769572">
<source UID="OBJECT_2016080819041598947781" />
<data type="LabelSetImage" file="ifbaaa_Bolus.nrrd" />
<properties file="ticaaa" />
</node>
后面跟着更多的节点。
我在第3行第0列看不到任何垃圾?我认为必须有另一个错误原因。
.xml文件由外部软件MITK生成,所以我认为应该没问题。
使用Win 7,64位,VS2015,Anaconda
答案 0 :(得分:14)
正如@Matthias Wiehl所说,ElementTree只需要一个根节点,而不是格式良好的XML,应该在其原点修复。 作为一种解决方法,您可以在文档中添加假根节点。
var version = "?v=0.0.1";
// array of URL's to request
var links = ["app/css/normalize.min.css", "app/css/bootstrap.min.css", ..];
$.holdReady(true);
$.when.apply($, $.map(links, function(path) {
return new $.Deferred(function(d) {
$("<link>", {href: path + version, rel: "stylesheet"})
.appendTo("head").on("load", d.resolve);
})
}))
.then(function() {
$.holdReady(false)
});
$(document).ready(function() {
// do stuff when all `<link>` elements have been appended to `<head>`
})
答案 1 :(得分:2)
文档的根节点(Version
)在第2行打开和。解析器不期望在根节点之后有任何节点。解决方案是删除关闭正斜杠。
答案 2 :(得分:0)
尝试像这样修复文档。关闭末尾的version
元素
<?xml version="1.0" encoding="UTF-8" ?>
<Version Writer="E:\d\src\Modules\SceneSerialization\src\mitkSceneIO.cpp" Revision="$Revision: 17055 $" FileVersion="1">
<node UID="OBJECT_2016080819041580480127">
<source UID="OBJECT_2016080819041550469454" />
<data type="LabelSetImage" file="hfbaaa_Bolus.nrrd" />
<properties file="sicaaa" />
</node>
<node UID="OBJECT_2016080819041512769572">
<source UID="OBJECT_2016080819041598947781" />
<data type="LabelSetImage" file="ifbaaa_Bolus.nrrd" />
<properties file="ticaaa" />
</node>
</Version>