0

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.

Dirty Bird Design
  • 5,333
  • 13
  • 64
  • 121
  • 1
    Your xml contains xml, so you'll have to first parse the wrapper xml, then parse the xml string contained within it. – Kevin B Mar 07 '14 at 19:21
  • I thought jQuery's $.parseXML did that? – Dirty Bird Design Mar 07 '14 at 19:24
  • 1
    No, it will only parse the outer xml doc, not nested xml docs. (not quite sure why a service would return an xml document wrapper for an xml document.) – Kevin B Mar 07 '14 at 19:28
  • I dont think it is ideal, but its what I have to work with. I will make the suggestion to the developer who made the service, seems strange to me as well – Dirty Bird Design Mar 07 '14 at 19:30

1 Answers1

1

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();
Kevin B
  • 94,570
  • 16
  • 163
  • 180
  • As usual, that is awesome. Thanks man. Would you just wrap this all in a function and call a setTimeout to get it to refresh? – Dirty Bird Design Mar 07 '14 at 19:31
  • Yes, just don't make the timeout too short. I wouldn't go any less than 30 seconds – Kevin B Mar 07 '14 at 19:32
  • When i wrap it like this in a setTimeout, it delays the initial load and doesn't refresh... any input? setTimeout(function() { $( ".priceData" ).append( $price.attr("Value") ); }, 15000); – Dirty Bird Design Mar 07 '14 at 19:43
  • 1
    You need to resend the whole ajax request. All of the code in your question would need to be within that setTimeout (minus the static variable containing the url of course) – Kevin B Mar 07 '14 at 19:57
  • http://jsfiddle.net/2N3Dy/1/ when I do it inside the setTimeout, it waits 5 seconds to load and does not refresh... what am I missing? – Dirty Bird Design Mar 07 '14 at 20:08
  • setTimeout only happens once, i meant setInterval. – Kevin B Mar 07 '14 at 20:09
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/49269/discussion-between-dirty-bird-design-and-kevin-b) – Dirty Bird Design Mar 07 '14 at 20:11
  • Really appreciate it, still can't get it to load initially each time it waits the assigned time to load once then it refreshes as desired – Dirty Bird Design Mar 07 '14 at 20:19
  • The second example doesn't have that problem. Hmm, i remember you from the jQuery forums, i haven't been there in a while. – Kevin B Mar 07 '14 at 20:20
  • Yeah, I recognized your logo from 'tentonaxe' but didn't want to appear stalkerish... I don't go to the jQuery forms as much, SO is better IMO. Ill give the second one a try again, thanks again for your help man. Really appreciate it. – Dirty Bird Design Mar 07 '14 at 20:29
  • 1
    Oops, i used your non-working code in my last two examples instead of my code. How'd that happen http://jsfiddle.net/Y8CcT/1/ – Kevin B Mar 07 '14 at 20:38