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 › Beginner: read multiple xml files
Page Index Toggle Pages: 1
Beginner: read multiple xml files (Read 503 times)
Beginner: read multiple xml files
Jan 13th, 2008, 10:02pm
 
I want to read out data from multiple xml-files. I created an Array for each xml-file (myArtist1 and myArtist2). I would like to read more than 2 xml files. But copying the same for-condition seems stupid to me. is there any way, so that i have to write this for-condition only once?


import processing.xml.*;

XMLElement xml1;
XMLElement xml2;

void setup() {

 size(800, 400);
 background(0);
 
 xml1 = new XMLElement(this, "artists1.xml");
 xml2 = new XMLElement(this, "artists2.xml");
 
 int numSites = xml1.getChildCount();
 
 String[][] myArtist1 = new String[numSites][3];
 
 
 for (int i=0; i < numSites; i++){
   XMLElement track1 = xml1.getChild(i);
   XMLElement[] siteData = track1.getChildren();
   String s1 = siteData[0].getContent();
   String s2 = siteData[2].getContent();
   String s3 = siteData[3].getContent();
   myArtist1[i][0] = s1;
   myArtist1[i][1] = s2;
   myArtist1[i][2] = s3;
 }
 
 String[][] myArtist2 = new String[numSites][3];
 
 for (int i=0; i < numSites; i++){
   XMLElement track2 = xml2.getChild(i);
   XMLElement[] siteData = track2.getChildren();
   String s1 = siteData[0].getContent();
   String s2 = siteData[2].getContent();
   String s3 = siteData[3].getContent();
   myArtist2[i][0] = s1;
   myArtist2[i][1] = s2;
   myArtist2[i][2] = s3;
 }

}
Re: Beginner: read multiple xml files
Reply #1 - Jan 14th, 2008, 6:44am
 
Use an array of XMLElements and merge your string arrays (myArtist1, myArtist2) into one :

Code:
import processing.xml.*;

XMLElement[] xml;

void setup() {

size(800, 400);
background(0);

xml[0] = new XMLElement(this, "artists1.xml");
xml[1] = new XMLElement(this, "artists2.xml");

int numSites = xml[0].getChildCount();

String[][][] myArtist = new String[numSites][3][xml.length];

for (int x=0; x < xml.length; x++) {

for (int i=0; i < numSites; i++) {
XMLElement track = xml[x].getChild(i);
XMLElement[] siteData = track.getChildren();
String s1 = siteData[0].getContent();
String s2 = siteData[2].getContent();
String s3 = siteData[3].getContent();
myArtist[i][0][x] = s1;
myArtist[i][1][x] = s2;
myArtist[i][2][x] = s3;
}

}

}
Re: Beginner: read multiple xml files
Reply #2 - Jan 14th, 2008, 9:35am
 
Doesnt work for me. Say: "NullPointerException" at

xml[0] = new XMLElement(this, "artists1.xml");
Re: Beginner: read multiple xml files
Reply #3 - Jan 14th, 2008, 10:25am
 
sorry, I forgot to initialize the array :

Code:
xml = new XMLElement[2];
xml[0] = new XMLElement(this, "artists1.xml");
xml[1] = new XMLElement(this, "artists2.xml");
Page Index Toggle Pages: 1