[英]Parsing XML to table with jQuery
I am using YQL to pull in a remote XML feed, I have this working in all other areas, but cannot get it to parse the data and format into a table. 我正在使用YQL来引入远程XML提要,我在其他所有领域都有这个工作,但无法将其解析为数据并将其格式化为表格。 Here is my fiddle .
这是我的小提琴 。 The xml looks like this:
xml看起来像这样:
<string xmlns="http://tempuri.org/">
<?xml version="1.0" encoding="utf-16"?>
<BTCE>
<TickerList />
<Ticker>
<Average Value="" />
<BuyPrice Value="443.95" />
<Currency Value="USD" />
<High Value="456.96" />
<Low Value="424.00" />
<SellPrice Value="444.27" />
<Volume Value="18754.79784877" />
<LastPrice Value="443.95" />
<Time Value="04/28/2014 15:56:54" />
</Ticker>
<Ticker>
<Average Value="" />
<BuyPrice Value="444.32" />
<Currency Value="USD" />
<High Value="456.96" />
<Low Value="424.00" />
<SellPrice Value="444.70" />
<Volume Value="18762.65028563" />
<LastPrice Value="443.96" />
<Time Value="04/28/2014 15:57:57" />
</Ticker>
<Ticker>
<Average Value="" />
<BuyPrice Value="444.32" />
<Currency Value="USD" />
<High Value="456.96" />
<Low Value="424.00" />
<SellPrice Value="445.00" />
<Volume Value="18758.16227820" />
<LastPrice Value="444.32" />
<Time Value="04/28/2014 15:58:08" />
</Ticker>
</BTCE>
I have the following HTML mark up: 我有以下HTML标记:
<table class="fluid" id="BuyOrders">
<tr>
<th>Price per/ BTC</th>
<th>Quantity, ฿</th>
<th>Total, $</th>
</tr>
</table>
Attempting to parse the xml like this: 试图像这样解析xml:
$(function () {
site = 'http://ec2-54-201-216-39.us-west-2.compute.amazonaws.com/testc/WebService.asmx/GetTicker';
var yql = 'http://query.yahooapis.com/v1/public/yql?q=' + encodeURIComponent('select * from xml where url="' + site + '"') + '&format=xml&callback=?';
function loadTable() {
$.getJSON(yql, function (data) {
var xml = $.parseXML(data.results[0]),
xmlDoc = $.parseXML($(xml).find("string").text()),
$xml = $(xmlDoc),
$buyPrice = $xml.find("BuyPrice");
$volume = $xml.find("volume");
$sellPrice = $xml.find("SellPrice");
var tr;
for (var i = 0; i < xml.length; i++){
tr = $('<tr/>');
tr.append('<td>' + $buyPrice.attr("Value") + '</td>');
$('#BuyOrders').append(tr);
}
});
}
loadTable();
});
It seems you want to loop over each Ticker
in the xml and add a row for each one with the relevant values: 看来你想循环遍历xml中的每个
Ticker
,并为每个Ticker
添加一个具有相关值的行:
function loadTable() {
$.getJSON(yql, function (data) {
var xml = $.parseXML(data.results[0]),
xmlDoc = $.parseXML($(xml).find("string").text()),
$xml = $(xmlDoc);
$xml.find("Ticker").each(function(){
var buyPrice = $(this).find("BuyPrice").attr("Value");
var tr = $("<tr/>");
tr.append("<td>" + buyPrice + "</td>");
/* ... any other tds here with various field values ... */
$("#BuyOrders").append(tr);
});
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.