simple example of reading website XML

So i have been scouring this site and others for a good way to pull down some xml from a website (I will deal with the formatting later) and put it on the screen. I was trying to use the NSSTring stuff then switched over to ofxiPhoneFile to use its geturl capability and nothing seems to work. I think my problem may be trying to understand how to deal with the data ofxiphonefile.read() is returning?

here is the key parts of my code:

  
#include "ofMain.h"  
#include "ofxiPhone.h"  
#include "ofxiPhoneFile.h"  
  
class testApp : public ofxiPhoneApp {  
	  
public:  
	void setup();  
	void update();  
	void draw();  
	void exit();  
	  
	void touchDown(ofTouchEventArgs &touch);  
	void touchMoved(ofTouchEventArgs &touch);  
	void touchUp(ofTouchEventArgs &touch);  
	void touchDoubleTap(ofTouchEventArgs &touch);  
	  
	void lostFocus();  
	void gotFocus();  
	void gotMemoryWarning();  
	  
	ofxiPhoneFile rawXML;  
	  
	string convertedXML;  
	ofTrueTypeFont verdana;  

and here is the .mm file:

  
#include "testApp.h"  
  
  
//--------------------------------------------------------------  
void testApp::setup(){	  
	// register touch events  
	ofRegisterTouchEvents(this);  
	  
	// initialize the accelerometer  
	ofxAccelerometer.setup();  
	  
	//iPhoneAlerts will be sent to this.  
	ofxiPhoneAlerts.addListener(this);  
	  
	ofSetColor(255,255,255);  
	  
	rawXML.openFromURL("[http://egauge119.local/cgi-bin/egauge?"](http://egauge119.local/cgi-bin/egauge?"));  
	  
	verdana.loadFont("verdana.ttf", 20);  
	printf("loaded");  
	  
}  
  
//--------------------------------------------------------------  
void testApp::update() {  
}  
  
//--------------------------------------------------------------  
void testApp::draw() {  
	printf("at the draw");  
	//char Str[1024]; //tried some conversions  
	//sprintf(Str, "%s", rawXML.readNextLine());  
	//printf(Str);  
	verdana.drawString(rawXML.readNextLine(), 15,30);  
}  
  
  

So I managed to figure it out and here it is if anyone is interested. requires the ofxiPhoneFile and ofxXmlSetings addons.

the testApp.h file

  
#include "ofMain.h"  
#include "ofxiPhone.h"  
#include "ofxiPhoneFile.h"  
#include "ofxXmlSettings.h"  
  
class testApp : public ofxiPhoneApp {  
	  
public:  
	void setup();  
	void update();  
	void draw();  
	void exit();  
	  
	void touchDown(ofTouchEventArgs &touch);  
	void touchMoved(ofTouchEventArgs &touch);  
	void touchUp(ofTouchEventArgs &touch);  
	void touchDoubleTap(ofTouchEventArgs &touch);  
	  
	void lostFocus();  
	void gotFocus();  
	void gotMemoryWarning();  
	  
	ofxiPhoneFile rawXML;  
	ofxXmlSettings XML;  
	  
	string convertedXML;  
	ofTrueTypeFont verdana;  
};  
  

the testApp.mm file

  
#include "testApp.h"  
  
  
//--------------------------------------------------------------  
void testApp::setup(){	  
	// register touch events  
	ofRegisterTouchEvents(this);  
	  
	// initialize the accelerometer  
	ofxAccelerometer.setup();  
	  
	//iPhoneAlerts will be sent to this.  
	ofxiPhoneAlerts.addListener(this);  
	  
	//call your website and store it  
	rawXML.openFromURL("[http://egauge118.d.egauge.net/cgi-bin/egauge?"](http://egauge118.d.egauge.net/cgi-bin/egauge?"));   
	  
	//put it into string form and store it as XML  
	XML.loadFromBuffer(string(rawXML.read()));  
	  
	verdana.loadFont("verdana.ttf", 10);  
	printf("loaded");  
	ofEnableAlphaBlending();  
	  
}  
  
//--------------------------------------------------------------  
void testApp::update() {  
}  
  
//--------------------------------------------------------------  
void testApp::draw() {  
	ofSetColor(255,255,255, 255);  
	  
	//push into the first tag and make it the "root"  
	XML.pushTag("measurements", 0);  
		//push into the second tag to make it root, use the number as an index for tags with the same name  
		//this would not be needed if this particular xml was not relying on titles which this xml library doesnt deal with  
		XML.pushTag("meter", 0);  
			int grid = XML.getValue("energy", 0, 0);  
		XML.popTag(); //pop back out to measurements  
		XML.pushTag("meter", 1); //now push into the SECOND meter tag  
			int solar = XML.getValue("energy", 0, 0);  
		XML.popTag();  
	XML.popTag(); //pop twice to take us all the way back out to the main root so that we are reset for the next time  
	  
	char tempChar [255]; //make temporary char array  
	sprintf(tempChar, "%i", grid); //turn the int into chars  
	verdana.drawString(tempChar, 15,30);  
	sprintf(tempChar, "%i", solar);  
	verdana.drawString(tempChar, 15,60);  
	ofSetColor(0, 255, 0, 175);  
	ofCircle(ofGetWidth()/2, ofGetHeight()/2, solar/2);  
	ofSetColor(255, 0, 0, 175);  
	ofCircle(ofGetWidth()/2, ofGetHeight()/2, grid/2);  
	  
}  
  
//--------------------------------------------------------------  
void testApp::exit() {  
}  
  
  
//--------------------------------------------------------------  
void testApp::touchDown(ofTouchEventArgs &touch){  
}  
  
//--------------------------------------------------------------  
void testApp::touchMoved(ofTouchEventArgs &touch){  
}  
  
//--------------------------------------------------------------  
void testApp::touchUp(ofTouchEventArgs &touch){  
}  
  
//--------------------------------------------------------------  
void testApp::touchDoubleTap(ofTouchEventArgs &touch){  
}  
  
  
//--------------------------------------------------------------  
void testApp::lostFocus() {  
}  
  
//--------------------------------------------------------------  
void testApp::gotFocus() {  
}  
  
//--------------------------------------------------------------  
void testApp::gotMemoryWarning() {  
}  

i’ve been trying to do this for a while too. my solution is to get the file, download it to a folder (all using objective-C, which i hate) and then use the ofxXMLSettings addon to parse the file.
your solution is quite simpler in terms of lines used.

rawXML.openFromURL("http://egauge119.local/cgi-bin/egauge?"); <- when you use this, does it write the file to disk or is it stored in memory?

it’s stored in memory

thanks for the quick reply.

this is great, no need to use objective-C and no need to download the file, read it and erase it from disk.
now to get things working in a thread…

i’ve been trying to parse XML files for ages, but doing it with objective C is strange. i’ve read a tutorial about parsing XML with the iPhone but i don’t get the app delegate parts and the making a mixed class with objective C and regular c++.

finally i’ll finish my app about the usage of profane words.

i am getting an error

" error: ‘class ofxiPhoneFile’ has no member named ‘openFromURL’ "

my ofxiPhoneFile is from the website and its ofxiPhoneFile_001.zip
i got no complaints about not finding the header file… and it loads the addon, because first i had forgotten to rename the .cpp files to .mm and had a bunch of errors.
is there a new ofxiPhoneFile version?

yeah, grab the one from here:
http://github.com/stfj/assorted-ofxiPhone-classes

Here is a simple class I put together that I use a lot, it may be of some help to everyone. Its completely C++ based (using Poco) and works great for me on iPhone (provided you’re using the version of ofxIphone that has poco).

The class is called ofxFileLoader and it basically has two functions. The first one takes a url and returns the contents of that URL as a string. This can then be used in a variety of ways, including loading into ofxXmlSettings. If the url is invalid/not found it returns an error message.

The second function takes a url and a filename, and it will save the contents of the url into the filename/path. It returns a boolean for success or failure.

to use the class you’d do something like this

  
ofxFileLoader loader;   
ofxXmlSettings xml;  
  
string xmlFromServer = loader.loadFromUrl("[http://www.myserver.com/myXML.xml"](http://www.myserver.com/myXML.xml"));  
xml.loadFromBuffer(xmlFromServer);  
//or  
loader.loadFromUrl"[http://www.myserver.com/myXML.xml","myXML.xml](http://www.myserver.com/myXML.xml","myXML.xml));  
xml.loadFile("myXML.xml");  
  

I’ve used this class on a number or projects and it has worked great, its mostly just a hacked together collection of code found on the forum in a nice small package.

Hope that helps, I like going this route because it keeps everything a little less iPhone dependent and lets me keep my code base consistent across all platforms.

-Steve

ofxFileLoader.zip

bit of a newbee… can you provide a sample project file for ofxFileLoader. preferably in visual studio but anything would be helpful. Thanks!

This is exactly what i was looking for. Thanks for the tools.