Question

I found a solution here on SO for retrieving cross domain xml files, however I am unable to parse the returned data. I also need to set a timeout function on this in order to keep it refreshing - it is price/voulume data.

//the remote xml
site = 'http://ec2-54-201-216-39.us-west-2.compute.amazonaws.com/testb/WebService.asmx/GetTickerFromBtcE';
var yql = 'http://query.yahooapis.com/v1/public/yql?q=' + encodeURIComponent('select * from xml where url="' + site + '"') + '&format=xml&callback=?';

$.getJSON(yql, function (data) {
    var xml = $.parseXML(data.results[0]),
        xmlDoc = $.parseXML( xml ),
        $xml = $( xmlDoc ),
        $price = $xml.find( "Currency Value");
        $( "#data" ).append( $price.text() );
    console.log(xml);
});

a simple fiddle is here

it appears in the console under #document, as a string, I dont know if that is correct or not. It seesm like that could be an issue as well as the tag names having spaces in them e.g. "BuyPrice Value"

Ive read several other questions here and unfortunately I don't think the back end developer will deliver in jsonp, which would alleviate a lot of this. Also, what would be the best method to get this to refresh every XX minutes? Any advice is greatly appreciated.

Was it helpful?

Solution

First, you have to deal with the fact that you have an xml document inside your xml document.

var xml = $.parseXML(data.results[0]),
    xmlDoc = $.parseXML( $(xml).find("string").text() ),

Then you want to get the Currency node's Value attribute.

    $xml = $( xmlDoc ),
    $price = $xml.find("Currency");
$( "#data" ).append( $price.attr("Value") );

Final Result:

var xml = $.parseXML(data.results[0]),
    xmlDoc = $.parseXML( $(xml).find("string").text() ),
    $xml = $( xmlDoc ),
    $price = $xml.find("Currency");
$( "#data" ).append( $price.attr("Value") );

http://jsfiddle.net/F52a5/1/

Two ways of refreshing it:

site = 'http://ec2-54-201-216-39.us-west-2.compute.amazonaws.com/testb/WebService.asmx/GetTickerFromBtcE';
var yql = 'http://query.yahooapis.com/v1/public/yql?q=' + encodeURIComponent('select * from xml where url="' + site + '"') + '&format=xml&callback=?';
setInterval(function(){
    $.getJSON(yql, function (data) {
        var xml = $.parseXML(data.results[0]),
            xmlDoc = $.parseXML($(xml).find("string").text()),
            $xml = $(xmlDoc),
            $price = $xml.find("Currency");
        $("#data").append($price.attr("Value"));
    });
},30*1000);

Or, the preferred method:

site = 'http://ec2-54-201-216-39.us-west-2.compute.amazonaws.com/testb/WebService.asmx/GetTickerFromBtcE';
var yql = 'http://query.yahooapis.com/v1/public/yql?q=' + encodeURIComponent('select * from xml where url="' + site + '"') + '&format=xml&callback=?';
function getNewData() {
    $.getJSON(yql, function (data) {
        var xml = $.parseXML(data.results[0]),
            xmlDoc = $.parseXML($(xml).find("string").text()),
            $xml = $(xmlDoc),
            $price = $xml.find("Currency");
        $("#data").append($price.attr("Value"));
        setTimeout(getNewData,30*1000);
    });
}
getNewData();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top