我正在尝试使用 Javascript/Jquery 将一些 XML 数据解析为 JSON,以便在 Highcharts 项目中使用。不幸的是,我无法弄清楚我的代码出了什么问题,因为它甚至无法读取 XML。到目前为止我已经:
xml:
<Row>
<Category>data</Category>
<actual>data</actual>
</row>
....
HTML:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>Document</title>
</head>
<body>
<h1>Hello</h1>
<div id="container" style="height: 400px; width: 500px"></div>
<script type = "text/javascript" src = "jquery-1.11.1.min.js"></script>
<script src="http://code.highcharts.com/highcharts.js"></script>
<script type = "text/javascript" src = "test.js"></script>
</body>
</html>
Javascript:
$(document).ready(function(){
var globalData = new Array();
// $("h1").click(function(){
// Load the data from the XML file
$.get('C:\\Users\\xxxxxx\\Desktop\\xmloutput.xml', function(xml) {
alert("it works");
// Split the lines
var $xml = $(xml);
// push series
$xml.find('Row').each(function(i, row) {
var seriesOptions = {
Category: $(series).find('Category').text(),
Actual: $(series).find('Actual').text(),
};
// add it to the options
globalData.push(seriesOptions);
});
});
// });
$(function() {
var chart = new Highcharts.Chart({
chart: {
renderTo: 'container',
type: 'pie'
},
plotOptions: {
pie: {
borderColor: '#000000',
innerSize: '60%'
}
},
series: [{
data: globalData
}]
},
// using
function(chart) { // on complete
var xpos = '50%';
var ypos = '53%';
var circleradius = 102;
// Render the circle
chart.renderer.circle(xpos, ypos, circleradius).attr({
fill: '#ddd',
}).add();
// Render the text
chart.renderer.text('THIS TEXT <span style="color: red">should be in the center of the donut</span>', 155, 215).css({
width: circleradius*2,
color: '#4572A7',
fontSize: '16px',
textAlign: 'center'
}).attr({
// why doesn't zIndex get the text in front of the chart?
zIndex: 999
}).add();
});
});
});
我相信我的实际问题可能是我的 xml 解析语法不正确,但在 Firefox 的开发控制台上运行它没有显示任何错误。希望这里的专家能够发现问题
感谢您的宝贵时间。
最佳答案
我看到的一个直接问题是使用本地路径
$.get('C:\\Users\\xxxxxx\\Desktop\\xmloutput.xml',
$.get
第一个参数是 url,它是网络上的位置,它可能类似于 $.get('http://localhost/xmls/xmloutput.xml,...
关于javascript - 将 XML 解析为 JSON 时遇到问题 - Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24515216/