Using string from XML is causing problems.
in
Programming Questions
•
1 year ago
Dear all interested readers,
I am trying to combine two sketches I have. The first sketch is a tool to make make a box fit a text.
code:
This works perfect.
Now I have the next code for loading news articles from my server:
(For this code, I am using the SimpleML library | http://www.shiffman.net/p5/simpleML/simpleML.zip )
What I want is to draw the found articles like the first sketch.
What I did is combine the text with getting articles:
void netEvent(XMLRequest ml) {
// Retrieving an array of all XML elements inside" title* "tags
String[] headlines = ml.getElementArray( "artikel");
String[] datum = ml.getElementArray( "datum");
String[] keyword = ml.getElementArray( "keywords");
for (int i = 0; i < headlines.length; i++ ) {
println(headlines[i]);
println(datum[i]);
fill(0,255,0);
rect(10, 10, specificWidth+20, textboxhoogte(string, specificWidth, fontSize, lineSpacing));
fill(0);
text(string, 20, 20, specificWidth+10, height);
translate(0,textboxhoogte(string, specificWidth, fontSize, lineSpacing));
}
//
This gives me an error. What can I do best? Right now, I am really stuck and I don't know why.
Thanks in advance for your help.
I am trying to combine two sketches I have. The first sketch is a tool to make make a box fit a text.
code:
- import processing.opengl.*;
PFont font;
int fontSize = 30;
int specificWidth = mouseX+5;
int lineSpacing = 11;
int textHeight;
void setup() {
size(600, 600, OPENGL);
background(0);
font = createFont("Arial", fontSize);
textFont(font, fontSize);
noLoop();
}
void draw() {
String string = "This is where the article that is recieved from the XML should apear. It should automaticly get the right height to fit the text. After this, the next article should be drawn under this textframe";
background(0);
int specificWidth = mouseX+5;
fill(0,255,0);
rect(10, 10, specificWidth+20, textboxhoogte(string, specificWidth, fontSize, lineSpacing));
fill(0);
text(string, 20, 20, specificWidth+10, height);
translate(0,textboxhoogte(string, specificWidth, fontSize, lineSpacing));
loop();
}
int textboxhoogte(String string, int specificWidth, int fontSize, int lineSpacing) {
String[] wordsArray;
String tempString = "";
int numLines = 0;
float textHeight;
wordsArray = split(string, " ");
for (int i=0; i < wordsArray.length; i++) {
if (textWidth(tempString + wordsArray[i]) < specificWidth) {
tempString += wordsArray[i] + " ";
}
else {
tempString = wordsArray[i] + " ";
numLines++;
}
}
numLines++; //adds the last line
textHeight = numLines * (textDescent() + textAscent() + lineSpacing);
return(round(textHeight));
}
This works perfect.
Now I have the next code for loading news articles from my server:
import simpleML.*;
XMLRequest xmlRequest;
XMLRequest keywords;
void setup() {
size(200, 200);
keywords = new XMLRequest(this, "http://robotfunk.com/temp/getall.php" );
keywords.makeRequest();
xmlRequest = new XMLRequest(this, "http://robotfunk.com/temp/getall.php?keyword[]=&keyword[]=Amsterdam" );
xmlRequest.makeRequest();
}
void draw() {
noLoop(); // Nothing to see here
}
// When the request is complete
void netEvent(XMLRequest ml) {
// Retrieving an array of all XML elements inside" title* "tags
String[] headlines = ml.getElementArray( "artikel");
String[] datum = ml.getElementArray( "datum");
String[] keyword = ml.getElementArray( "keywords");
for (int i = 0; i < headlines.length; i++ ) {
println(headlines[i]);
println(datum[i]);
}
println(keyword);
println("totaal aantal artikelen:");
println(headlines.length);
}
(For this code, I am using the SimpleML library | http://www.shiffman.net/p5/simpleML/simpleML.zip )
What I want is to draw the found articles like the first sketch.
What I did is combine the text with getting articles:
void netEvent(XMLRequest ml) {
// Retrieving an array of all XML elements inside" title* "tags
String[] headlines = ml.getElementArray( "artikel");
String[] datum = ml.getElementArray( "datum");
String[] keyword = ml.getElementArray( "keywords");
for (int i = 0; i < headlines.length; i++ ) {
println(headlines[i]);
println(datum[i]);
fill(0,255,0);
rect(10, 10, specificWidth+20, textboxhoogte(string, specificWidth, fontSize, lineSpacing));
fill(0);
text(string, 20, 20, specificWidth+10, height);
translate(0,textboxhoogte(string, specificWidth, fontSize, lineSpacing));
}
//
This gives me an error. What can I do best? Right now, I am really stuck and I don't know why.
Thanks in advance for your help.
1