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 › XML database - It's my birthday :)
Page Index Toggle Pages: 1
XML database - It's my birthday :) (Read 1073 times)
XML database - It's my birthday :)
Mar 28th, 2010, 2:52pm
 
hi everybody,

I have a question about how to parse this file:
http://paste2.org/p/743176

i cannot get the information of this file. Did i make this XML file wrong?

or must it be like this:
<move player="0" timeFirst="60" timeLast="62" dice1"2" dice2="3" total"5" fromPlace "1" toPlace"5"></move>



here's my code
Code:

XMLElement xml;

void setup() {
 //size(500, 500);
 background(0);
 smooth();
 
 loadXML();
 
}

void loadXML() {
 xml = new XMLElement(this, "XML_final_2.xml");
 int num_dots = xml.getChildCount();

 
 for (int i=0; i<num_dots; i++) {
   XMLElement d = xml.getChild(i);
   
//   int game = d.getIntAttribute("game");
  int move = d.getIntAttribute("move");
 int player = d.getIntAttribute("player");
  int timeFirst = d.getIntAttribute("timeFirst");
  int timeLast = d.getIntAttribute("timeLast");
int dice1 = d.getIntAttribute("dice1");
int dice2 = d.getIntAttribute("dice2");
    int total = d.getIntAttribute("total");
    int fromPlace = d.getIntAttribute("fromPlace");
    int toPlace = d.getIntAttribute("toPlace");
   
   


println(d.getChild(1).getContent());

}}

 


ow, it's my birthday today Smiley
Re: XML database - It's my birthday :)
Reply #1 - Mar 28th, 2010, 7:58pm
 
Happy Birthday
Re: XML database - It's my birthday :)
Reply #2 - Mar 29th, 2010, 2:26am
 
I have little experience with Processing's XML parser, but I believe your code is wrong in several ways...

I will go back to this thread later, to do some testing if you need more help, but for now, I just give some hints/suppositions.
- The child of game is moves. The children of moves are each move. Then you have children of move, the various tags.
- The values are not attributes, but content. Use getContent(). Or refactor your XML as you shown.
- Note: you can have several identical elements, eg. two <dice> elements.
Re: XML database - It's my birthday :)
Reply #3 - Mar 30th, 2010, 8:40am
 
Is that backgammon data? Smiley
Here is code to parse the XML and store it in a structured way (you might want to change this structure, perhaps replacing MovePair with a simple array of two elements):
Code:
XMLElement xml;

void setup()
{
 ReadXMLData();
 println(moveList);
 exit();
}

ArrayList moveList = new ArrayList();
class MovePair
{
 Move movePlayer1;
 Move movePlayer2;

 void Add(Move move)
 {
   if (move.player == 0)
   {
movePlayer1 = move;
   }
   else  // Assume always a pair of 'move' per 'moves'
   {
movePlayer2 = move;
   }
 }

 String toString()
 {
   return "<" + movePlayer1 + ", " + movePlayer2 + ">";
 }
}
class Move
{
 int player;
 int timeFirst;
 int timeLast;
 int dice1;
 int dice2;
 int total;
 int fromPlace;
 int toPlace;

 void Add(String tag, String value)
 {
   if (tag.equals("player"))
   {
player = Integer.valueOf(value);
   }
   else if (tag.equals("timeFirst"))
   {
timeFirst = Integer.valueOf(value);
   }
   else if (tag.equals("timeLast"))
   {
timeLast = Integer.valueOf(value);
   }
   else if (tag.equals("dice1"))
   {
dice1 = Integer.valueOf(value);
   }
   else if (tag.equals("dice2"))
   {
dice2 = Integer.valueOf(value);
   }
   else if (tag.equals("total"))
   {
total = Integer.valueOf(value);
   }
   else if (tag.equals("fromPlace"))
   {
fromPlace = Integer.valueOf(value);
   }
   else if (tag.equals("toPlace"))
   {
toPlace = Integer.valueOf(value);
   }
   else
   {
println("Unknown tag: " + tag + " with value: " + value);
   }
 }

 String toString()
 {
   return player + "[(" +
 timeFirst + ", " + timeLast + "), (" +
 dice1 + ", " + dice2 + "), " +
 total +
 fromPlace + ", " + toPlace + ")]";
 }
}

void ReadXMLData()
{
 xml = new XMLElement(this, "XML_final_2.xml"); // game
//~   println(xml.getName());
 for (int i = 0; i < xml.getChildCount(); i++)
 {
   XMLElement movesElt = xml.getChild(i); // moves
//~     println(moves.getName());
   MovePair mp = new MovePair();
   moveList.add(mp);
   for (int j = 0; j < movesElt.getChildCount(); j++) // Always 2?
   {
XMLElement moveElt = movesElt.getChild(j); // move
//~ println(moveElt.getName());
Move move = new Move();
for (int k = 0; k < moveElt.getChildCount(); k++)
{
 XMLElement e = moveElt.getChild(k);
 String t = e.getName();
 String c = e.getContent();
 move.Add(t, c);
}
mp.Add(move);
   }
 }
}

Happy birthday!
Page Index Toggle Pages: 1