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{    
        void setup();    
        void update();    
        void draw();    
       ofxXmlSettings xml;  
        void urlResponse(ofHttpResponse &httpResponse);    

void testApp::setup(){    
void testApp::urlResponse(ofHttpResponse &httpResponse){    
    if(httpResponse.status==200 ){  // i.e is it ok  
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:

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

void testApp::keyPressed(int key){  
    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:


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:


Joshua - you are a legend, thanks.