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.
Page Index Toggle Pages: 1
Parsing arrays (Read 2327 times)
Parsing arrays
May 5th, 2005, 1:42am
 
Hi, thanks to Fjen for help on getting to this point firstly. I'm trying to parse a String array that contains some XML data, and return the number of entries that contain a certain word (Such as "fred"). I looked through the Processing documentation, but can't find a good way to do this..The code looks like this:

Code:
			
String c = ((XMLElement)rec_info[s]).getContent().toString();
String[] d = new String[records.length];


d[i] = c;


Are there any pointers or tips on how to do this?

best,

Tim
Re: Parsing arrays
Reply #1 - May 5th, 2005, 5:50pm
 
i'm not sure i totally understand what you are doing ... count the nodes whichs content _equals_ or _contains_ a search-string?
... and is it count or find the nodes?

F
Re: Parsing arrays
Reply #2 - May 5th, 2005, 6:58pm
 
It is to count the number of times - so in an array

Code:

Object [] records


that contains all of my XML records which, when printed to the screen look like this:

Code:

<RECORD><chapter>Bauhaus to Desktop</chapter><title>Bahaus I</title><example>Nice picture</example></RECORD>

<RECORD><chapter>Meta-Media</chapter><title>meta-data</title><example>Elements in Meta-Data</example></RECORD>

<RECORD><chapter>Representing IT Society</chapter><title>Computation</title><example>Casey Reas</example></RECORD>

...


What I want to do is count the number of instances that a phrase, such as "Representing IT Society", appears. I thought that I can implement some sort of counter that would do the trick.

Later in the code, the content (what is inside each <RECORD> tag is parsed to a String array..The code looks like this:

Code:

for (int i=0; i< records.length; i++)
{

//println(records.length);
// Determines the x-coordinate of where an individual chapter begins based on the Nucleus' x-position + sin*numeric radian value
kidx=centerNucleus.nx+radius*sin(radians(angle)*i);

// Determines the y-coordinate of where an individual chapter begins based on the Nucleus' y-position + cos*numeric radian value
kidy=centerNucleus.ny+radius*cos(radians(angle)*i);

// If the nucleus has been clicked, then the chapter nodes, lines, and section lines and nodes get redrawn based on the x,y coordinates
// of the mouse. Otherwise, nothing besides the center nucleus will be drawn on the screen

Object[] rec_info = ((XMLElement)records[i]).getChildren().toArray();


if (nucleusclicked == true)
{
if (i <= 13)
{
stroke(255); // Sets color of line from Nucleus to Chapter (0 = black, 255 = white)
line(centerNucleus.nx, centerNucleus.ny, kidx, kidy); // Draws a line from the Nucleus' position to where the node for chapter[i] will be created
noFill(); // Fill color for chapter node
chapterNode[i].drawChapter(kidx,kidy,noderadius); // Draws the actual chapter node
}
for( int r=0; r<rec_info.length; r++)
{

if( r == 0)
{
for( int s=0; s<1; s++)
{
//println( ((XMLElement)rec_info[r]).getContent());

String c = ((XMLElement)rec_info[r]).getContent().toString();
String[] d = new String[records.length];

d[i] = c;

//println( ((XMLElement)rec_info[r]).getContent());

if( ((XMLElement)rec_info[r]).getName().equals("chapter"))
{
fill(123,30,134);

if( (i > 13) )
{
// print nothing
} else {
text(d[i],kidx,kidy-noderadius);
}
}

//println( ((XMLElement)rec_info[r]).getContent());
//println(chaptertext[i]); // Prints the name of each individual chapter
//fill(255); // Chapter text COLOR
//text(chaptertext[i], kidx,kidy-noderadius); // Writes the text from the chaptertext array where that individual chapter is positioned
//noFill(); // Section node COLOR
//chapters[i].drawkids(kidx,kidy,radius/2); //Draws SECTION lines and NODES for each CHAPTER
//println(d[i]);
}
}
}
}

}


So that it iterates (at the moment) through only the <CHAPTER> tags to retrieve the content.
Re: Parsing arrays
Reply #3 - May 5th, 2005, 11:44pm
 
so ... i assume you are looking for a way to count nodes whichs content equals a given string. then you need a flexible sort of array which can grow (add new phrases) and lets you update and retrieve the data in relation to the phrase. this is not easily doable in java ... simplest i found is wrapping some functions around java.util.Hashtable:
Code:

void setup()
{
size( 100, 100 ); noLoop();

hsh = new Hashtable();

hSet( "fred", 1 );
hSet( "dan", 1);
hAdd( "fred", 1 );

println( hGet( "fred" ) + " / " + hGet( "dan" ) );
}

void draw()
{

}

Hashtable hsh;
void hSet ( String _s, int _i )
{
hsh.put( _s, new Integer(_i) );
}

int hGet ( String _s )
{
return ((Integer)(hsh.get(_s))).intValue();
}

void hAdd ( String _s, int _i )
{
hSet( _s, hGet( _s )+_i );
}


hashtable-doc is here:
http://java.sun.com/j2se/1.4.2/docs/api/java/util/Hashtable.html

hope this helps ..

F
Page Index Toggle Pages: 1