Reputation: 2247
I have a excel file (xls) and I want to show that data in charts on a web page.
I can save the file as a xml document in Excel but i don't now how to parse it.
$(document).ready(function() {
$.ajax({
url: "data.xml",
type: "GET",
dataType: "xml",
success: function(xml){
parseXml(xml);
}
});
});
function parseXml(xml)
{
var xml = $(xml);
var data = xml.find("Worksheet").children('table').children('row').children('Cell');
console.log(data);
}
This works "kind of" but to get the right values is hard.
For example, if I want to get the Net Sales which looks like this in the xml document:
<Row>
<Cell ss:StyleID="s44">
<Data ss:Type="String">Net sales</Data>
</Cell>
<Cell ss:StyleID="s45">
<Data ss:Type="Number">11779.0</Data>
</Cell>
<Cell ss:StyleID="s45">
<Data ss:Type="Number">10996.0</Data>
</Cell>
<Cell ss:StyleID="s46"/>
<Cell ss:StyleID="s45">
<Data ss:Type="Number">10222.0</Data>
</Cell>
<Cell ss:StyleID="s46"/>
<Cell ss:StyleID="s45">
<Data ss:Type="Number">11636.0</Data>
</Cell>
<Cell ss:StyleID="s45">
<Data ss:Type="Number">10918.0</Data>
</Cell>
<Cell ss:StyleID="s42"/>
<Cell ss:StyleID="s45">
<Data ss:Type="Number">10231.0</Data>
</Cell>
<Cell ss:StyleID="s45">
<Data ss:Type="Number">9649.0</Data>
</Cell>
<Cell ss:StyleID="s46"/>
<Cell ss:StyleID="s45">
<Data ss:Type="Number">10871.0</Data>
</Cell>
</Row>
Upvotes: 0
Views: 1848
Reputation: 28974
Since jQuery 1.5, there's built-in support for parsing an XML string to a jQuery object. Just use jQuery.parseXML
and then you can query the resulting jQuery object just as if it were a DOM.
Due to the same origin policy (see HTTP access control on MDC) I was prevented from doing a cross-browser GET, so I had to copy the the source code from your XML file and put it in a textarea.
Could be be that any escaping you had in there was lost because I am getting a parse error. The attribute ss:Format
contains an invalid value:
_-* #,##0.00\ _k_r_-;\-* #,##0.00\ _k_r_-;_-* "-"??\ _k_r_-;_-@_-
I removed this value, and it worked like a charm! See my test case on jsFiddle.
Upvotes: 2