Help using a website's API that returns XML *SOLVED*

Hi there,

I have recently moved form processing where I could use the following to get info from echonest’s API as an XML file:

  
xml = new XMLElement(this, query);  

The query string would contain the web address that would return XML.

I think I need to use ofHTTPRequest() but I just can’t work it out…

Thanks, Matt

To grab an XML file you can use the ofxXmlSettings addons, make an ofxXmlSettings object and the ofLoadURL method like so:

  
#pragma once    
    
#include "ofMain.h"    
#include "ofxXmlSettings.h"  
  
class testApp : public ofBaseApp{    
    
    public:    
        void setup();    
        void update();    
        void draw();    
  
       ofxXmlSettings xml;  
        void urlResponse(ofHttpResponse &httpResponse);    
        
    
};    

  
void testApp::setup(){    
    ofRegisterURLNotification(this);  
    ofLoadURLAsync(URL,"load");    
}    
    
void testApp::urlResponse(ofHttpResponse &httpResponse){    
    if(httpResponse.status==200 ){  // i.e is it ok  
        xml.loadFile(httpResponse.data.getText());  
    }  
}    
    
void testApp::update(){    
    
}    
    
void testApp::draw(){    
    
}    
     
  

Parsing through XML is a little bit weird b/c of tinyXML, but take a look at the addonExample and definitely ask q’s if you have them.

Thanks for the reply Joshua!

I have used your code as it is with the following URL:

http://developer.echonest.com/api/v4/artist/biographies?api-key=N6E4NIOVYMTHNDM8J&id=ARH6W4X1187B99274F&format=xml&results=1&start=0&license=cc-by-sa

And I’ve used the following code just to check what is being returned:

  
void testApp::keyPressed(int key){  
	xml.copyXmlToString(xmlString);  
    cout << xmlString << endl;  
	  
	}  

Unfortunately the string is empy… have also tried saving the file as a .xml and it is empty. Any ideas?

Thanks again, Matt

My mistake (should have checked before typing that), it should be:

  
xml.loadFromBuffer(httpResponse.data);  

ofxXmlSettings::loadFile() expects a file path, not a buffer or string. loadFromBuffer() lets you just pass a buffer to it. Oops, at least I won’t get those two conflated again :slight_smile:

Bingo.

Joshua - you are a legend, thanks.

M