2014-07-01 45 views
0

我想解析一些XML数据到JSON中使用JavaScript/Jquery在Highcharts项目中使用。不幸的是,我无法弄清楚我的代码有什么问题,因为它甚至不会读取XML。到目前为止,我有:麻烦解析XML到JSON-Javascript

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的开发控制台上运行它显示没有错误。希望这里的专家能够发现问题

感谢您的时间。

+0

您需要使用Web服务器通过加载文件看$ .get()函数。 –

回答

2

一个imediate问题,我可以使用本地路径

$.get('C:\\Users\\xxxxxx\\Desktop\\xmloutput.xml', 

$.get第一个参数是URL,它在网络位置也可能是像$.get('http://localhost/xmls/xmloutput.xml,...

+0

不解决问题,因为我看不到任何新的事情发生。也许意味着我有更多的代码问题:( – YazanLpizra

+0

你可以从你的web应用程序中的路径读取xml,把你想读取的xml文件放到网站代码所在的文件夹中,然后输入http:// localhost [port你正在使用]/xmloutput.xml'。网站不能看到'C:'只有在它的文件夹内的东西。 –

+0

有没有默认的端口,我会不知不觉地使用?这个网站只是一个原型,现在是尚未托管 – YazanLpizra