We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpOther Libraries › Using the proXML load method more than once
Page Index Toggle Pages: 1
Using the proXML load method more than once? (Read 942 times)
Using the proXML load method more than once?
Mar 4th, 2010, 12:37pm
 
I will try to keep this as clear and as simple as possible.

I want to create a flickr app that displays info on 100 recent interesting photos. I've started with some sample code from Bryan Chung (http://www.bryanchung.net/?p=189) that displays 100 photos.

He does this by loading an XML element which automatically calls the Event method.  In the Event Method is another method which takes the xml element and passes it through to yet another method and so on and so on.


My question is this. If I want to then take the xml information I've received and then create a new xml object to make another call (to get info on the photos, for example), how do I do it in a way so that a new Event method is called.

I know this sounds confusing. Maybe take a look at the code?



import proxml.*;

String apiKey = "  "; // I've removed my apiKey and nsid
String nsid = "  ";
String url = "http://api.flickr.com/services/rest/";
XMLInOut xml = null;
XMLInOut xml2 = null;
boolean gettingPhoto = true;
String photoID = "";
int choose = 0;
int numberOfPhotos = 10;

void setup() {
 size(1000,1500);
 background(0);
 frameRate(15);
 xml = new XMLInOut(this);
   xml2 = new XMLInOut(this);
}

void draw() {
}





void keyPressed() {
 
 if (key == ' '){
 nsid = "";
 findByUsername();
 
 
 
 }
 
 if (key == 'q') { choose = choose + 1; println (choose);}
 if (key == 'w') { choose = choose - 1; println (choose);}
 
}

// Here we load a URL to our xml variable which will be sent to flickr to get 100 of the most recent interesting photos
void findByUsername() {
 
 if (gettingPhoto){
 String rest = url+"?method=flickr.interestingness.getList";
 rest += "&api_key=" + apiKey;
 xml.loadElement(rest);
 }
 
 else {
  String rest = url+"?method=flickr.photos.getInfo";
 rest += "&api_key=" + apiKey + "&photo_id="  + photoID  ;
 xml2.loadElement(rest);


 }
 
}








// RIGHT BELOW HERE IS THE PROBLEM!!!


void xmlEvent(proxml.XMLElement _x) {
 parseXML(_x);
}













void parseXML(proxml.XMLElement _x) {
 String stat = _x.getAttribute("stat");
 if (!stat.equals("ok")) {
   println("Error from Flickr");
   return;
 }
 proxml.XMLElement node = _x.getChild(0);
 String type = node.getName();
 if (type.equals("user")) {
   nsid = node.getAttribute("nsid");
   println(nsid);

 }
 else if (type.equals("photos")) {
   int num = node.countChildren();
   println(num);
   getPhotos(node);
 }
}









void getPhotos(proxml.XMLElement _n) {
 int cnt = _n.countChildren();
 cnt = min(cnt,numberOfPhotos);
 for (int i=0;i<cnt;i++) {

   proxml.XMLElement ph = _n.getChild(i);
   String fm = ph.getAttribute("farm");
   String sv = ph.getAttribute("server");
   String id  = ph.getAttribute("id");
   String sc = ph.getAttribute("secret");
   String imgURL = "http://farm"+fm+".static.flickr.com/"+
     sv + "/" + id + "_" + sc + "_s.jpg";
   PImage img = loadImage(imgURL);
   int x = (i%5) * img.width;
   int y = (i/5) * img.height;
   image(img,x,y);
 
   
   
 }

  gettingPhoto = !gettingPhoto;

}












Any help would be very much appreciated.

Thanks!




Re: Using the proXML load method more than once?
Reply #1 - Mar 4th, 2010, 1:24pm
 
can't find anything in the way of proxml documentation so i'm guessing here...

is the argument to
xml = new XMLInOut(this);
the name of the thing handling the xml events? if so, have the second lot of xml use a different handler class.

or is the second xml full of different elements? you could use the same xmlEvent and switch on the content.
Re: Using the proXML load method more than once?
Reply #2 - Mar 4th, 2010, 3:32pm
 
thanks koogy for the quick reply...

I am attempting to do what you suggest, but since I'm a novice at this you may need to spell it out for me (literally). How would I go about making the changes you think would help?
Re: Using the proXML load method more than once?
Reply #3 - Mar 5th, 2010, 12:12am
 
well, like i said, i can't find proxml docs or, indeed, the library and i don't have it installed. but the only way i can see of being able to handle two different files are the two things i mentioned.

btw, there's xml support built into processing. what does the proxml library give you that the builtin version doesn't?

http://processing.org/reference/XMLElement.html

ah, ok, library links have been updated and i can see the proxml documentation.

so, i am *guessing* you need

interestingXML = new XMLInOut(this, InterestingHandler);

then have a new class, InterestingHandler*, that has a void xmlEvent method that decodes the Interesting XML. another similar class for the second bit of xml (btw, calling things xml and xml2 isn't the best of ideas)

* this may need to be an instance rather than a classname, documentation isn't clear and there's no example.
Re: Using the proXML load method more than once?
Reply #4 - Mar 6th, 2010, 1:32am
 
Thank you so much Koogy!

Your suggestion has totally worked. Nice.

In case anyone is curious, below is my current code. It is unfinished, so it might appear a bit messy, but it does solve the problem that we were talking about. Of course, feel free to use it.



import proxml.*;

int numberOfPhotos = 10;


PFont font;
String apiKey = "    ";
String nsid = "  ";
String url = "http://api.flickr.com/services/rest/";
XMLInOut xml = null;
XMLInOut infoXml = null;

String [] fm = new String [numberOfPhotos];
String [] sv = new String [numberOfPhotos];
String [] sc = new String [numberOfPhotos];
String [] photoID = new String [numberOfPhotos];
String [] photoURL = new String [numberOfPhotos];
String [] title = new String [numberOfPhotos];
String [] li = new String [numberOfPhotos];
int choose = 0;
int counter = 0;



Info info = new Info();

void setup() {
 font = loadFont("Futura-Medium-12.vlw");
 size(1000,1500);
 background(0);
 frameRate(15);
 xml = new XMLInOut(this);
 infoXml = new XMLInOut(this,info);
   
   for (int i = 0; i < numberOfPhotos; i++){
     
    photoID [i]= "";
    photoURL [i]= "";
   
   }
   
   getInterestingPhotos();
   
}

void draw() {
 
 
 
 
}





void keyPressed() {
 
 if (key == 'i'){
nsid = "";

println(photoID[0]);
//info.findInfo(photoID[0]);
getInfo();



 }
 
   if (key == 'l'){


//println(info.l);




 }
 
   if (key == ' '){
 nsid = "";
 
printPhotos();
printMainPhoto(choose);
 }
 
 if (key == 'q') { if(choose < numberOfPhotos -1){ choose = choose + 1; println (choose); printMainPhoto(choose);}}
 if (key == 'w') { if(choose > 0){choose = choose - 1; println (choose); printMainPhoto(choose);}}
 
}

// Here we load a URL to our xml variable which will be sent to flickr to get 100 of the most recent interesting photos
void getInterestingPhotos() {
 

 String rest = url+"?method=flickr.interestingness.getList";
 rest += "&api_key=" + apiKey;
 xml.loadElement(rest);
 println("xml1 called");
 
}


void getInfo () {
  String rest = url+"?method=flickr.photos.getInfo";
 rest += "&api_key=" + apiKey + "&photo_id="  + photoID[1] ;
  println("REST  "  + rest);
 
  //String rest = url+"?method=flickr.interestingness.getList";
 //rest += "&api_key=" + apiKey;
 
 infoXml.loadElement(rest);
 
}




void xmlEvent(proxml.XMLElement _x) {
 parseXML(_x);
}







void parseXML(proxml.XMLElement _x) {
 String stat = _x.getAttribute("stat");
 if (!stat.equals("ok")) {
   println("Error from Flickr");
   return;
 }
 proxml.XMLElement node = _x.getChild(0);
 String type = node.getName();
 if (type.equals("user")) {
   nsid = node.getAttribute("nsid");
   println(nsid);

 }
 else if (type.equals("photos")) {
   int num = node.countChildren();
   println(num);
   getPhotos(node);  println("photos node");
   
   
   
 }
}









void getPhotos(proxml.XMLElement _n) {
 
 println ("prePhoto print method");
 int cnt = _n.countChildren();
 cnt = min(cnt,numberOfPhotos);
 for (int i=0;i<cnt;i++) {

   proxml.XMLElement ph = _n.getChild(i);
   fm [i] = ph.getAttribute("farm");
   sv [i] = ph.getAttribute("server");
   photoID[i] = ph.getAttribute("id");
   sc [i] = ph.getAttribute("secret");
   title [i] = ph.getAttribute("title");
   //li [i] = ph.getAttribute("username");
   //println( "This is license " + li[i]);
   photoURL[i] = "http://farm"+fm[i]+".static.flickr.com/"+
     sv[i] + "/" + photoID[i] + "_" + sc[i] + "_s.jpg";

 //println();
 //println(i + " " + photoURL[i]);
 println (i + " " + title[i] + "   id: " + photoID[i]);
   
   
 }


}




void printPhotos (){
 
for (int i = 0; i < numberOfPhotos; i++){  
   PImage img = loadImage(photoURL[i]);
   //println();
   //println(photoURL[i]);
   int x = (i%5) * img.width;
   int y = (i/5) * img.height;
   image(img,x,y);

  }
}



void printMainPhoto (int chosen){
   noStroke();
   fill(0);
   rect(400, 0, 500,300);
   String bigPhoto = photoURL[chosen] = "http://farm"+fm[chosen]+".static.flickr.com/"+
     sv[chosen] + "/" + photoID[chosen] + "_" + sc[chosen] + "_m.jpg";
   PImage img = loadImage(bigPhoto);
   //println(photoURL[i]);
   fill(255);
   textFont(font);
   smooth();
   text(title[chosen], 400,30);
   image(img,400,50);


}











public class Info {

int numberOfPhotos = 10;

String apiKey = "d1a7da3cf57e7fd369fffa90d9b06ffc";
String nsid = "7e0c62548999a06b";
String url = "http://api.flickr.com/services/rest/";
XMLInOut xml = null;
Object a = new Object();



String [] fm = new String [numberOfPhotos];
String [] sv = new String [numberOfPhotos];
String [] sc = new String [numberOfPhotos];
String [] photoID = new String [numberOfPhotos];
String [] photoURL = new String [numberOfPhotos];
String [] title = new String [numberOfPhotos];
String [] li = new String [numberOfPhotos];
int choose = 0;
int counter = 0;
String var = new String();

Info () {var = null;}




void xmlEvent(proxml.XMLElement _x) {
 parseXML(_x);
 println("info event method triggered");
}







void parseXML(proxml.XMLElement _x) {
 String stat = _x.getAttribute("stat");
 if (!stat.equals("ok")) {
   println("Error from Flickr - INFO");
   return;
 }
 else {println ("info stat OK");}
 proxml.XMLElement node = _x.getChild(0);
 String type = node.getName();
 if (type.equals("user")) {
   nsid = node.getAttribute("nsid");
   println(nsid);

 }
 else if (type.equals("photo")) {
   int num = node.countChildren();
   println(num);

   getInfo(node); println("gettingInfo");}
   
   
 }
}


void getInfo(proxml.XMLElement _n) {
 
 println("getInfo called");
 
 int cnt = _n.countChildren();
 cnt = min(cnt,numberOfPhotos);
 //for (int i=0;i<cnt;i++) {

   //proxml.XMLElement ph = _n.getChild(i);
       proxml.XMLElement ph = _n;
   
   println(ph);
   //li[i] = ph.getAttribute("title");
     String
Page Index Toggle Pages: 1