0

我从一些 jquery xml 解析中得到了意外的行为。

我正在使用以下代码从服务器获取文件

$.ajax({
    type: "GET",
    url: "//path/test.xml",
    dataType: "xml",
    success: function(xml) { //do stuff }

它没有按预期执行,如果我直接从文件中获取数据并将其硬编码到我的 JS 中,它会按预期工作。文件中的数据格式为

<?xml version="1.0" encoding="utf-8"?><ProductRates xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" Total="64" Region="UK"><ProductRate><Description>UAE - Dirham</Description><ProductType>CUR</ProductType><CurrencyCode>AED</CurrencyCode><Rate>5.4427</Rate></ProductRate><ProductRate><Description>Australia - Australia Dollar</Description><ProductType>CUR</ProductType><CurrencyCode>AUD</CurrencyCode><Rate>1.6726</Rate></ProductRate></ProductRates>

所以一旦我得到了xml,它就会像这样处理

var $xml = $(xml);
var $list = $('#ticker'); 
$prodtype = $xml.find("ProductType");
$prodtype.each(function() {
var self = $(this);
if( self.text() == "CUR") { 
    var 
        $CurrencyCode = self.next('CurrencyCode')
        $Rate         = $CurrencyCode.next('Rate')          
    ; 
    $( "#ticker" ).append("<li><a>" +$CurrencyCode.text()+" = "+ $Rate.text()+ " </a></li>");

}

});

我正在使用来自http://www.gcmingati.net/wordpress/wp-content/lab/jquery/newsticker/jq-liscroll/scrollanimate.html的 liScroll js/css ,它确实有效,但列表项垂直堆叠并且不是水平穿过股票代码容器。可能是如何解析 xml 文件?

页面运行时关联的 css,希望对您有所帮助...

element.style {
background-image: url(test.img);
}
Matched CSS Rules
currency_ticker_test.shtmlmedia="all"
#content-header {
background-repeat: no-repeat;
background-position: center top;
}
currency_ticker_test.shtmlmedia="all"
html, body, div, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, acronym, address, big, cite, code, del, dfn, em, font, img, ins, kbd, q, s, samp, small, strike, strong, sub, sup, tt, var, b, u, i, center, dl, dt, dd, ol, ul, li, fieldset, form, label, legend, table, caption, tbody, tfoot, thead, tr, th, td {
margin: 0;
padding: 0;
border: 0;
outline: 0;
font-size: 100%;
vertical-align: baseline;
background: transparent;
}
user agent stylesheetdiv {
display: block;
}
Inherited from div#shell
currency_ticker_test.shtmlmedia="all"
html, body, div, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, acronym, address, big, cite, code, del, dfn, em, font, img, ins, kbd, q, s, samp, small, strike, strong, sub, sup, tt, var, b, u, i, center, dl, dt, dd, ol, ul, li, fieldset, form, label, legend, table, caption, tbody, tfoot, thead, tr, th, td {
font-size: 100%;
}
Inherited from body
currency_ticker_test.shtmlmedia="all"
body {
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
color: #2f2f2f;
}
currency_ticker_test.shtmlmedia="all"
body {
line-height: 1;
}
currency_ticker_test.shtmlmedia="all"
html, body, div, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, acronym, address, big, cite, code, del, dfn, em, font, img, ins, kbd, q, s, samp, small, strike, strong, sub, sup, tt, var, b, u, i, center, dl, dt, dd, ol, ul, li, fieldset, form, label, legend, table, caption, tbody, tfoot, thead, tr, th, td {
font-size: 100%;
}
Inherited from html
currency_ticker_test.shtml media="all"
html, body, div, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, acronym, address, big, cite, code, del, dfn, em, font, img, ins, kbd, q, s, samp, small, strike, strong, sub, sup, tt, var, b, u, i, center, dl, dt, dd, ol, ul, li, fieldset, form, label, legend, table, caption, tbody, tfoot, thead, tr, th, td {
font-size: 100%;
}
4

1 回答 1

1

您的 xml 结构无效。您有两个打开的 ProductRates 元素。

<ProductRates xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" Total="64" Region="UK">
<ProductRates>

检查有效 xml 的最佳方法是将输出保存到 xml 文件并在浏览器中打开。

于 2013-09-02T13:00:23.233 回答