2014-01-19 30 views
1

我試圖通過Google Apps腳本將http://indicador.eof.cl/rss XML feed分析到Gooogle網站,以解析forex值(所有這些值)。使用Google Apps腳本解析來自XML元素的所有值?

腳本如下>

function doGet(){ 

    var response = UrlFetchApp.fetch("http://indicador.eof.cl/rss").getContentText(); 
    var parsedResponse = Xml.parse(response, false); 
    var root = parsedResponse.getElement(); 
    var entries = root.getElement('channel').getElements("item"); 

    for (var i=0; i<entries.length; i++) { 
     var e = entries[i]; 
     var title = e.getElement("title").getText(); 
     var description = e.getElement("description").getText(); 
    } 

    var app = UiApp.createApplication(); 
    var TopVar = app.createHorizontalPanel(); 

    TopVar.add(app.createLabel(title).setStyleAttribute("fontSize","12px")); 
    TopVar.add(app.createLabel(description).setStyleAttribute("fontSize","12px")); 

    app.add(TopVar); 
    return app; 
} 

的問題是,代碼只是給我的第一個值沒有所有的人,就是我忘了?

最好的問候,

回答

0

嘗試中移動TopVar.add(...);線循環:

var app = UiApp.createApplication(); 
var TopVar = app.createHorizontalPanel(); 

for (var i=0; i<entries.length; i++) { 
    var e = entries[i]; 
    var title = e.getElement("title").getText(); 
    var description = e.getElement("description").getText(); 
    TopVar.add(app.createLabel(title).setStyleAttribute("fontSize","12px")); 
    TopVar.add(app.createLabel(description).setStyleAttribute("fontSize","12px")); 
} 

其實,我什麼都不知道谷歌 - 應用程序腳本。但是你現在的代碼邏輯看起來有點偏離。它不使用局部變量的值在循環內聲明(e,titledescription)。這些變量的值在每次迭代中都改變了,沒有任何代碼使用它。

+0

非常感謝你的工作。 其他問題,我怎樣才能優化這段代碼從XML提取只提取一些外匯價值? – user3212216

+0

沒關係,歡迎來到StackOverflow。如果您發現它正常運行,請不要忘記[接受答案](http://meta.stackexchange.com/a/5235)。這是這個網站的工作方式:http://stackoverflow.com/help/someone-answers – har07

+0

對不起,我不能進一步,因爲這可能是平臺的具體問題。我知道如何在C#中完成它(* e.getElements('item')。where(some-criteria-here)*),但不在javascript/gas平臺中。考慮發佈另一個問題,讓其他具有更多js/gas專業知識的成員可以幫助你。 – har07