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 & HelpPrograms › XML getName If Statement not working
Page Index Toggle Pages: 1
XML getName If Statement not working (Read 2973 times)
XML getName If Statement not working
Feb 5th, 2009, 7:03am
 
I am woking on a small project just by myself, working on simple game ideas I want to pursue. What I have now is that when I try to get the name of the xml tag I am loading and set it to a string I cannot use it in a if statement to check what it is describing(attribute loading code). I can do a println and grab the data from it, although it will not return true for some godforsaken reason? Also I do get an error in the console when it is run. Most likely this is an error of my own Ignorance I am unable to see.

XML File:
Code:

<?xml version="1.0" encoding="UTF-8" ?>
<polygon>

<map title="Default Map" version="100">Shit about map</poly>

<poly id="0" x="100" y="400" w="600" h="300" c="100">big blue rect</poly>

<poly id="1" x="500" y="500" w="400" h="100" c="200">small red rect</poly>

<poly id="2" x="300" y="200" w="100" h="100" c="200">small red rect</poly>
</polygon>

My sketch:
Code:

import processing.opengl.*;

XMLElement xmlMap; //defines xml string
int[][] polyData; //defines array
int polyId; //what poly were on
int polyPCount=5; //how many attributes each polyId has
int polyCount; //defined--setup in setup(obviously)

int gravity = -5;
int selfX = 200; //SOLDIERS COORDS
int selfY = 0; //SOLDIERS COORDS
int selfW = 30; //SOLDIERS WIDTH
int selfH = 50; //SOLDIERS HEIGHT
int veloX = 0;


void setup(){
size(800, 600, OPENGL);
frameRate(45);

xmlMap = new XMLElement(this, "maps/sites.xml"); //loads map xml data
polyCount = xmlMap.getChildCount(); //how many polys are there
polyData = new int[polyCount][polyPCount]; //makes a 2d array first is poly id 2nd level is 5 attributes
XMLElement[] polyName = xmlMap.getChildren(); //test
int curPoly=0; //current polygon we are parsing
int i=0; //current number were on
while(i < polyCount) { //runs through once for each poly
XMLElement polygon = xmlMap.getChild(curPoly);
String pieceName = polyName[i].getName(); //test
println(pieceName);
if(polyName[i].getName() == "map"){
println("ok nik");
}
if(polyName[i].getName() == "poly"){
polyData[curPoly][0] = polygon.getIntAttribute("x"); //x
polyData[curPoly][1] = polygon.getIntAttribute("y"); //y
polyData[curPoly][2] = polygon.getIntAttribute("h"); //height
polyData[curPoly][3] = polygon.getIntAttribute("w"); //width
polyData[curPoly][4] = polygon.getIntAttribute("c"); //color
//LOG TO CONSOLE
println("Poly " + i + ":");
println(" x axis:" + polyData[curPoly][0]);
println(" y axis:" + polyData[curPoly][1]);
println(" height:" + polyData[curPoly][2]);
println(" width:" + polyData[curPoly][3]);
println(" color:" + polyData[curPoly][4]);
println(pieceName);
curPoly++;
}/*else{
//LOG TO CONSOLE
println("Poly " + i + ":");
println(" x axis:" + polyData[curPoly][0]);
println(" y axis:" + polyData[curPoly][1]);
println(" height:" + polyData[curPoly][2]);
println(" width:" + polyData[curPoly][3]);
println(" color:" + polyData[curPoly][4]);
println(pieceName);
}*/
i++;
}
}


This is just the init and setup() although it should be all thats needed.
And the Error:
Code:

Closing tag does not match opening tag: `poly' != `map', SystemID='file:.', Line=3

at processing.xml.XMLUtil.errorWrongClosingTag(XMLUtil.java:533)

at processing.xml.StdXMLParser.processElement(StdXMLParser.java:601)

at processing.xml.StdXMLParser.scanSomeTag(StdXMLParser.java:284)

at processing.xml.StdXMLParser.processElement(StdXMLParser.java:625)

at processing.xml.StdXMLParser.scanSomeTag(StdXMLParser.java:284)

at processing.xml.StdXMLParser.scanData(StdXMLParser.java:226)

at processing.xml.StdXMLParser.parse(StdXMLParser.java:199)

at processing.xml.XMLElement.parseFromReader(XMLElement.java:235)

at processing.xml.XMLElement.<init>(XMLElement.java:212)

at xmltest.setup(xmltest.java:39)

at processing.core.PApplet.handleDraw(PApplet.java:1383)

at processing.core.PApplet.run(PApplet.java:1311)

at java.lang.Thread.run(Thread.java:619)

Re: XML getName If Statement not working
Reply #1 - Feb 5th, 2009, 10:12am
 
Code:

<map title="Default Map" version="100">Shit about map[b]</poly>[/b]


That </poly> should be </map>



My sketch:
Code:

   if(polyName[i].getName() == "map"){
     println("ok nik");
   }
   if(polyName[i].getName() == "poly"){


You can't compare strings with ==, you have to use

Code:
if(polyName[i].getName().equals("map"))

Re: XML getName If Statement not working
Reply #2 - Feb 23rd, 2009, 12:26pm
 
or use for strings

if (match (polyName[i].getName(), "map") != null){

{

but equals is more exact
Re: XML getName If Statement not working
Reply #3 - Feb 23rd, 2009, 1:13pm
 
meatless wrote on Feb 23rd, 2009, 12:26pm:
but equals is more exact

And much faster! And more intuitive (returning a boolean)...
Page Index Toggle Pages: 1