【发布时间】:2021-09-17 00:26:18
【问题描述】:
我正在尝试从文本文件创建数据表,但出现 XML Parsing Error: not well-formed。
我使用的文本文件包含两个以# 开头的标题行。然后每一行都有几列,我对前八列感兴趣。 该文件的摘录是:
# Retrieval interval Variable1 Variable2 Variable3
# start time end time Delta total Delta total Delta total
2020-12-05T00:00:00.128Z 2020-12-05T23:59:59.378Z 0.001765 59.738120 0.001765 59.808426 0.040705 59.940714
2020-12-05T23:59:59.378Z 2020-12-06T23:59:59.378Z 0.001418 59.736702 0.001418 59.807008 -0.024148 59.964862
2020-12-07T00:00:00.128Z 2020-12-07T23:59:59.378Z 0.001597 59.735105 0.001597 59.805411 0.024475 59.940387
2020-12-08T00:00:00.128Z 2020-12-08T23:59:59.378Z 0.001554 59.733551 0.001554 59.803857 -0.004524 59.944911
2020-12-09T00:00:00.128Z 2020-12-09T23:59:59.378Z 0.001776 59.731775 0.001776 59.802081 -0.003322 59.948233
2020-12-10T00:00:00.128Z 2020-12-10T23:59:59.378Z 0.000620 59.731155 0.000620 59.801461 0.017073 59.931160
2020-12-11T00:00:00.128Z 2020-12-11T23:59:59.378Z 0.001743 59.729412 0.001743 59.799718 -0.018987 59.950147
2020-12-12T00:00:00.128Z 2020-12-12T23:59:59.378Z 0.000973 59.728439 0.000973 59.798745 0.033104 59.917043
2020-12-13T00:00:00.128Z 2020-12-13T23:59:59.378Z 0.001660 59.726779 0.001660 59.797085 -0.026506 59.943549
2020-12-14T00:00:00.128Z 2020-12-14T23:59:59.378Z 0.001125 59.725654 0.001125 59.795960 0.055134 59.888415
2020-12-15T00:00:00.128Z 2020-12-15T23:59:59.378Z 0.000924 59.724730 0.000924 59.795036 -0.031429 59.919844
2020-12-15T23:59:59.756Z 2020-12-16T23:59:59.757Z 0.001986 59.722744 0.001885 59.793151 0.023362 59.896482
2020-12-17T00:00:00.128Z 2020-12-17T23:59:59.756Z 0.003619 59.719125 0.003571 59.789580 -0.002108 59.898590
2020-12-18T00:00:00.128Z 2020-12-18T23:59:59.756Z 0.001208 59.717917 0.001152 59.788428 -0.013224 59.911814
2020-12-19T00:00:00.128Z 2020-12-19T23:59:59.757Z 0.001437 59.716480 0.001366 59.787062 0.032044 59.879770
2020-12-20T00:00:00.128Z 2020-12-20T23:59:59.756Z 0.001406 59.715074 0.001333 59.785729 -0.021751 59.901521
2020-12-21T00:00:00.128Z 2020-12-21T23:59:59.757Z 0.001067 59.714007 0.001012 59.784717 0.043564 59.857957
2020-12-22T00:00:00.128Z 2020-12-22T23:59:59.757Z 0.001155 59.712852 0.001101 59.783616 -0.033792 59.891749
当我硬编码数据的值时:
var data_to_table = [
[
"2013-11-22T00:00:00.000Z",
"2013-11-22T00:00:00.000Z",
"0.000000",
"105.500000",
"0.000000",
"105.500000",
"0.000000",
"105.500000"
],
[
"2013-11-22T00:00:00.000Z",
"2013-11-25T00:00:00.000Z",
"0.342000",
"105.158000",
"0.342000",
"105.158000",
"1.900000",
"103.600000"
]
我得到了正确的表,但是当我从文件本身中挖掘数据时,我得到了这个错误消息。我已经检查了一些console.log(),我从函数data2json 中得到了一个数组。
我不是js 开发人员,我对这种错误有点迷茫。
这是我使用的代码,位于名为fuel2json.js 的文件下:
$(document).ready(function() {
$('#fuelTable').DataTable( {
data: data2json("processDump.fuel_history")
} );
},
function(error){
console.log(error)
} );
function data2json(file){
var array_data = [];
$.ajax({
url:file,
dataType:"text",
success:function(data){
var fuel_data = data.split("\n");
for(var count = fuel_data.length-1; count>=0; count--)
{
if( fuel_data[count].charAt(0) !== '#')
{
var cell_data = fuel_data[count].split(/\s+/);
if(cell_data.length > 1){
array_data.push(cell_data.slice(0,8))
};
};
};
}
});
return array_data
};
我的html文件是:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<body>
<table id="fuelTable" class="display" style="width:100%" data-order='[[0,"desc"]]'>
<thead>
<tr>
<th>Start</th>
<th>End</th>
<th>VAL1</th>
<th>VAL2</th>
<th>VAL3</th>
<th>VAL4</th>
<th>VAL5</th>
<th>VAL6</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Start</th>
<th>End</th>
<th>VAL1</th>
<th>VAL2</th>
<th>VAL3</th>
<th>VAL4</th>
<th>VAL5</th>
<th>VAL6</th>
</tr>
</tfoot>
</table>
<script type="text/javascript" src="../js/jquery.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/1.10.25/js/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="./fuel2json.js"></script>
</body>
</html>
【问题讨论】:
-
一些注意事项:(1) 我无法重新创建您遇到的特定“xml 解析错误”。 (2) 您的 JavaScript 代码没问题 - 它正确地将 JSON 中的数据解析为 JS 数组(您已经知道,我敢肯定)。 (3) 因为您是从 DataTables 调用 jQuery ajax,这是一个异步调用,所以函数中的数据不会很快提供给您的 DataTable - 表已经构建。 (4) 要处理第 3 项,我建议您将 ajax 调用移至 DataTable。当我这样做时,您的数据就会正确显示。
-
因为上面的问题(1),我们需要多看你的代码。你能告诉我们你正在使用的 HTML 吗?以及源数据文件的示例?
-
我已经编辑了这个问题,以便按照@andrewjames 的要求包含 html 和一些数据文件的摘录。这里的问题是我不知道如何将 ajax 调用移动到 DataTable... 正如我所说的我不是 javascript、ajax... 开发人员,我很难知道它是如何工作的(异步调用,承诺...)
-
感谢您的编辑。不幸的是,我仍然无法重新创建您的 XML 错误。我的代码版本运行正常。我注意到一些事情:(a) HTML prolog 通常只需要
<!DOCTYPE html>。不需要声明的其余部分(假设 HTML5 适合您)。 (b) 我没有看到 DataTabales CSS 资源 - 我只看到 3 个 JS 资源。 (c) 所有这些资源都应该在网页的 ...` 部分。但是这些都不能解释你得到的错误。
标签: javascript datatables xml-parsing