really cant get my head around XML feed and keyword queries- please help
in
Programming Questions
•
6 months ago
Hi there,
After a day of trying I really could do with some help. I know it's a common issue but I've tried to apply almost all relevant answers I could find, but I just can't get it right.
I can get an xml feed, I can do a keyword query in twitter ( thank you Forum!!) but I can't do a keyword querie in xml feeds.
(I'm after comparing all types of data queries and experimenting with their graphic expressions)
Here is the code I've 'constructed so far':
One day I'll hopefully be able to return the favors!!!
//-------------------------------------
String[] title;
//to highten the chance for something to show up I put in "have"
String[] good= {
"good", "peace", "growth", "have"
};
//to highten the chance for something to show up I put in "no"
String[] bad = {
"bad", "war", "cuts", "not"
};
XML xml;
PFont font;
ArrayList words;
//------------------------------------------------------
void setup() {
size(700, 700);
background(0);
// Setup font
font = loadFont("SansSerif-14.vlw");
textFont(font, 14);
words.add("good");
words.add("pece");
words.add("growth");
words.add("have");
words.add( "bad");
words.add("war");
words.add("cuts");
words.add("no");
ArrayList <String> words = new ArrayList <String> ();
// Load RSS feed in this case BBC news
xml = loadXML( "http://feeds.bbci.co.uk/news/rss.xml");
// Get title of each element
XML[] title = xml.getChildren("channel/item/title");
//----------------------------------
try {
QueryResult result = xml.search(title); //?????
ArrayList title = (ArrayList) result.getTitles();
title = new String[titlexml.length];
for (int i = 0; i < titlexml.length; i++) {
String title = titlexml[i].getContent();
String[] input = title.split(" ");
for (int j = 0; j < input.length; j++) {
//Put each word into the words ArrayList
words.add(input[j]);
}
}
}
catch (Exception te) { // for when it goes wrong
println("Couldn't connect: " + te);
};
}
//------------------------------------------------------
void draw() {
background(255);
//Draw a word from the list of words that we've built
int i = (frameCount % words.size());
String word = words.get(i);
//show the results
if (compareWordToArray(word, good)) {
fill(0, 255, 0);
text("good", random(width), random(height));
}
if (compareWordToArray(word, bad)) {
fill(255, 0, 0);
text("bad", random(width), random(height));
}
}
//------------------------------------------------------
boolean compareWordToArray(String word, String[] wordArray) {
for (int i=0; i<wordArray.length; i++) {
if (word.equals(wordArray[i])) return true;
}
return false;
}
After a day of trying I really could do with some help. I know it's a common issue but I've tried to apply almost all relevant answers I could find, but I just can't get it right.
I can get an xml feed, I can do a keyword query in twitter ( thank you Forum!!) but I can't do a keyword querie in xml feeds.
(I'm after comparing all types of data queries and experimenting with their graphic expressions)
Here is the code I've 'constructed so far':
One day I'll hopefully be able to return the favors!!!
//-------------------------------------
String[] title;
//to highten the chance for something to show up I put in "have"
String[] good= {
"good", "peace", "growth", "have"
};
//to highten the chance for something to show up I put in "no"
String[] bad = {
"bad", "war", "cuts", "not"
};
XML xml;
PFont font;
ArrayList words;
//------------------------------------------------------
void setup() {
size(700, 700);
background(0);
// Setup font
font = loadFont("SansSerif-14.vlw");
textFont(font, 14);
words.add("good");
words.add("pece");
words.add("growth");
words.add("have");
words.add( "bad");
words.add("war");
words.add("cuts");
words.add("no");
ArrayList <String> words = new ArrayList <String> ();
// Load RSS feed in this case BBC news
xml = loadXML( "http://feeds.bbci.co.uk/news/rss.xml");
// Get title of each element
XML[] title = xml.getChildren("channel/item/title");
//----------------------------------
try {
QueryResult result = xml.search(title); //?????
ArrayList title = (ArrayList) result.getTitles();
title = new String[titlexml.length];
for (int i = 0; i < titlexml.length; i++) {
String title = titlexml[i].getContent();
String[] input = title.split(" ");
for (int j = 0; j < input.length; j++) {
//Put each word into the words ArrayList
words.add(input[j]);
}
}
}
catch (Exception te) { // for when it goes wrong
println("Couldn't connect: " + te);
};
}
//------------------------------------------------------
void draw() {
background(255);
//Draw a word from the list of words that we've built
int i = (frameCount % words.size());
String word = words.get(i);
//show the results
if (compareWordToArray(word, good)) {
fill(0, 255, 0);
text("good", random(width), random(height));
}
if (compareWordToArray(word, bad)) {
fill(255, 0, 0);
text("bad", random(width), random(height));
}
}
//------------------------------------------------------
boolean compareWordToArray(String word, String[] wordArray) {
for (int i=0; i<wordArray.length; i++) {
if (word.equals(wordArray[i])) return true;
}
return false;
}
1