Javascript 用jQuery解析XML到表
我正在使用YQL拉入一个远程XML提要,我在所有其他领域都可以使用它,但无法让它解析数据并将其格式化为表。这是我的名片。xml如下所示:Javascript 用jQuery解析XML到表,javascript,jquery,xml,Javascript,Jquery,Xml,我正在使用YQL拉入一个远程XML提要,我在所有其他领域都可以使用它,但无法让它解析数据并将其格式化为表。这是我的名片。xml如下所示: <string xmlns="http://tempuri.org/"> <?xml version="1.0" encoding="utf-16"?> <BTCE> <TickerList /> <Ticker>
<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>
$(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();
});
我有以下HTML标记:
<table class="fluid" id="BuyOrders">
<tr>
<th>Price per/ BTC</th>
<th>Quantity, ฿</th>
<th>Total, $</th>
</tr>
</table>
尝试按如下方式解析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>
$(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();
});
似乎您希望循环xml中的每个Ticker,并为每个Ticker添加一行,其中包含相关值: 函数加载表{ $.getJSONyql,函数数据{ var xml=$.parseXMLdata.results[0], xmlDoc=$.parseXML$xml.findstring.text, $xml=$xmlDoc; $xml.findTicker.eachfunction{ var buyPrice=$this.findBuyPrice.attrValue; var tr=$; tr.append+buyPrice+; /*…此处具有各种字段值的任何其他tds*/ $BuyOrders.appendtr; }; }; }
为什么要使用.getJSON来检索XML?这是我在这里找到的一个解决方法,因此可以获得跨域xmlNo prob。我试图弄清楚如何在parse xml上运行.each函数来查找的每个实例,并解析其子实例并将它们附加到一行中。没有乐趣。我发现了这个问题——XML中只有一条记录,我们正试图在fiddle中检索,而不是像您在上面的示例中所显示的那样多次。发生在我们所有人身上——直到几分钟前我才想到检查返回。我用正确的服务更新了我的小提琴,现在返回了多个@dirtybirdesign,这不会改变代码。这段代码适用于多种情况:使用正确的服务和您的示例,它可以正常工作感谢您和@Jay Blanchard