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 & HelpSyntax Questions › Importing & Searching XML files.
Page Index Toggle Pages: 1
Importing & Searching XML files. (Read 730 times)
Importing & Searching XML files.
May 27th, 2009, 10:09am
 
Basic outline:

I want to download the latest tweets from Twitter, i'll be using the public timeline.I then want to search each tweet individually for certain words. depending on which words are in the tweet then it will out put a different value between 22- 53 and send it down the serial port to my Arduino. I've made a wall of light bulbs each with their own topic, each lighting circuit is connected to a pin on my Arduino (the pins go from 22 - 53). When I open each pin then it lights up the lightbulb on the circuit. SO the end effect is that i download the tweet, it searches it for key words, depending on what words are returned it will light up the appropriate bulb labels with the right topic.

What I need help with is importing the XML file, searching the file for the key words, and then outputting the right value.

I have no idea how to go about this and I have an extra problem of it needing to be finished by tomorrow morning, so if someone could help me I would be VERY VERY VERY grateful.



Thank you.
Re: Importing & Searching XML files.
Reply #1 - May 27th, 2009, 10:49am
 
Your first port of call should be the libraries.

Looks like you probably want proxml...

You'll need to identify the node that contains the data you're interested in and then check whether it contains the keyword.  It looks to me like proxml has all the methods you're likely to need.  Must admit I've tended to avoid XML, so not best placed to give specific advice, but at least this should put you on the right track Wink

Good luck!
Re: Importing & Searching XML files.
Reply #2 - May 27th, 2009, 1:49pm
 
thanks Smiley ill get on it!
Re: Importing & Searching XML files.
Reply #3 - May 27th, 2009, 3:46pm
 
I'm working on a Twitter project as well right now, so I thought I'd throw you this code to get you started. I avoided the proxml library and just went with the built in Processing XML importer. Here's some basic code which reads a Twitter feed, gets all the posts, and puts their content into an array.
Code:

void setup(){
 size(100,100);
 
//create a new XMLElement object and point it to the feed
 XMLElement element = new XMLElement(this, "http://twitter.com/statuses/user_timeline/14695737.rss");            
 
//create an XMLElement array to hold the list of children you're after
 XMLElement[] tweets = element.getChildren("channel/item/description");

//call getContent() to retrieve the text from the "description" tags
 for(int i=0; i<tweets.length; i++){
   println("\n"+tweets[i].getContent());
 }
}

void draw(){
 
}


Let me know if you have questions along the way. I'm doing this project for work, so I've been living and breathing this stuff for the past week.  Cheesy
Re: Importing & Searching XML files.
Reply #4 - May 27th, 2009, 5:13pm
 
hey Cheesy Thank you so much for that!

Would you happen to know how I would go about searching each tweet in the array for key words separately so that I can define an output based on which key words it has i.e if the tweet has a word to do with travel it outputs "1", technology "2" etc...

I think I need to use the match() function, but I'm unsure...

any ideas?
Re: Importing & Searching XML files.
Reply #5 - May 28th, 2009, 12:40am
 
Yes use match()

So assuming the content you want to search is in the "description" tags:

Code:
//call getContent() to retrieve the text from the "description" tags
 for(int i=0; i<tweets.length; i++){
   string myString = tweets[i].getContent();
   String[] m1 = match(myString, "someText");
   //etc... See match() reference
 }


Assuming I've understood the match() documentation correctly it looks like you can include all search terms in a single search - each one enclosed in parentheses - then iterate over the returned array - having first checked that it is not null - using conditions to check for occurrences of each relevant term and taking appropriate action.
Re: Importing & Searching XML files.
Reply #6 - May 28th, 2009, 2:34am
 
Avoid match if you don't master regular expressions, it might bite you back if you have even a simple dot in the searched string...
Use indexOf() instead, perhaps after applying toLowerCase().
Re: Importing & Searching XML files.
Reply #7 - May 28th, 2009, 5:10am
 
True, regular expressions can be a pain, but if he's really only searching for individual keywords I'd have thought it would be reliable; though having said that I was having problems with a test sketch this morning (mind you I was half-asleep).  Still I can see how indexOf could avoid additional conditions, so your suggestion is indeed a better option Wink
Re: Importing & Searching XML files.
Reply #8 - May 28th, 2009, 6:38am
 
For my project, I've been using a combination of indexOf() and substring() to isolate certain strings and parse them into new strings for testing. That combo works pretty well and is hassle free.

Oh, and Green Power Ranger, I'm not sure what your level of experience is, but in case you're not aware, we're getting some of these String methods from the straight Java String class, rather than the Processing API. You can still use these with no problem in Processing, but you won't find them in the reference. For a full list of fun stuff you can do with strings, check out the Java reference for Strings.
Page Index Toggle Pages: 1