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 Issues - Element name
Page Index Toggle Pages: 1
XML Issues - Element name (Read 1083 times)
XML Issues - Element name
May 26th, 2010, 6:42pm
 
I'm working on a processing thing where I need to integrate with an XML file. The problem is that the xml file has several levels...

<?xml version="1.0" encoding="ISO-8859-1"?>
<program>
   <item1>
       <spec1>0</spec1>
       <spec2>0</spec2>
   </item1>
   <item2>
       <spec2>0</spec1>
       <spec2>2</spec2>
   </item2>
</program>

I need a way to find out whether it's 'item1' or 'item2' I'm dealing with as they can come in a random order which makes this difficult. I tried to get the element name with getName but keep getting errors.

This is my code at the moment. It gives me the specs but not the item they go with. What I need to do is loop through the entire file and for each item1/item2 (etc) I need to find out the specs, do something with them and then go on to the next item.

   saved_layout = new XMLElement(this, "msg_layout.xml");
   level = saved_layout.getChildCount();
 for (int i=0; i<level ; i++){
     XMLElement tiles = saved_layout.getChild(i);
     int tile_children = tiles.getChildCount();
   for (int a=0; a<tile_children; a++){
     XMLElement children = tiles.getChild(a);
     println(children);
   }
 }


Hope this makes sense. Help would be much appreciated.


IceKat.
Re: XML Issues - Element name
Reply #1 - May 26th, 2010, 11:09pm
 
IceKat wrote on May 26th, 2010, 6:42pm:
       <spec2>0</spec1>
[...]
I tried to get the element name with getName but keep getting errors.

Note the XML error above.
When you get errors, it is useful to show them, particularly when you provide partial code.

I could get the following to work on the fixed XML (that's mostly your own code with getName() put at the right place):
Code:
void setup()
{
XMLElement saved_layout = new XMLElement(this, "D:/Documents/foo.xml");
int level = saved_layout.getChildCount();
for (int i = 0; i < level; i++) {
XMLElement tiles = saved_layout.getChild(i);
println("In " + tiles.getName());
int tile_children = tiles.getChildCount();
for (int a = 0; a < tile_children; a++) {
XMLElement children = tiles.getChild(a);
println(children);
}
}
exit();
}
Page Index Toggle Pages: 1