• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problems with variables when accessing XML file  RSS feed

 
Anja Schultz
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear All,
I have problems with variables when accessing an XML file. I definitley can set the variables (which I have tested by printing them on the screen) But when I try to use them, e. g. in other methods, they are still 0 or null. Following the two classes and the XML file.
What am I doing wrong?

public class Speed {

private int entryTime;
private int exitTime;
private int length;
private int speedLimit;
private int speed;
private String regno;


public void setEntry(String entry) {
entryTime = Integer.parseInt(entry);
}

public void setExit(String exit) {
exitTime = Integer.parseInt(exit);
//Just to test the variable value
System.out.println(exitTime);
}

public void setLength(String l) {
length = Integer.parseInt(l);
}

public void setSpeedLimit(String sl) {
speedLimit = Integer.parseInt(sl);
}

public void setRegno(String reg) {
regno = reg;
//Just to test the variable value
System.out.println(regno);
}

public int getSpeedLimit() {
return speedLimit;
}

public int getLength() {
return length;
}

void createReport() {

//calculate speed of car
speed = 3600 / 20; //Instead of the 20 there should be (exitTime - entryTime)
//but this always results into / 0. So, for testing, I
//replaced it with 20.
//if speed over limit print car details
if (speed > speedLimit) {
System.out.println("Reg no " + regno + " speed " + speed);
}
}
}







import org.xml.sax.*;
import java.io.*;
import java.util.*;

public class TrafficProcess extends HandlerBase{

//Place your variables here
String elementName;


public TrafficProcess() {

}

public static void main(String[] args) {
try {
System.out.println("...Starting");
Class loadedClass = Class.forName("com.ibm.xml.parser.SAXDriver");
Parser xParser = (Parser)loadedClass.newInstance();
TrafficProcess fp = new TrafficProcess();
xParser.setDocumentHandler(fp);
xParser.setErrorHandler(fp);
xParser.parse("traffic.txt"); //File holding XML DTD and source
}
catch(Exception e)
{System.out.println("Problem with XML processor "+e);}
}


public void error(SAXParseException se){
//No code needed here
}

public void warning(SAXParseException se){
//No code needed here
}

public void startDocument() throws SAXException{
}

public void startElement(String elementName, AttributeList al) throws SAXException{
this.elementName = elementName;
}

public void endElement(String elementName) throws SAXException{
//When the end of element EXITTIME is reached all variables needed to
//calculate the speed and create the report have been set.
//So we can call the appropriate methods now
if (elementName.equals("EXITTIME")) {
Speed s = new Speed();
s.createReport();
}
}

public void endDocument() throws SAXException{
Speed s = new Speed();
System.out.println("Data for M1 Roadworks north of Luton, speed limit "
+ s.getSpeedLimit() + ", length in miles is " + s.getLength());
}

public void characters(char[] chars, int start, int length)throws SAXException{
//Create a String variable stringValue and hand over the value of the
//current element. Then set each variable needed for speed calculation and
//comparison with the value of the corresponding element
String stringValue = new String(chars, start, length);
Speed s = new Speed();
if (elementName.equals("LENGTH"))
s.setLength(stringValue);
if (elementName.equals("SPEEDLIMIT"))
s.setSpeedLimit(stringValue);
if (elementName.equals("ENTRYTIME"))
s.setEntry(stringValue);
if (elementName.equals("EXITTIME"))
s.setExit(stringValue);
if (elementName.equals("REGNO"))
s.setRegno(stringValue);
}

}





<?xml version = "1.0" standalone = "yes"?>
<!DOCTYPE CARLIST[
><!ELEMENT LOCATION (#PCDATA)>
<!ELEMENT LENGTH (#PCDATA)>
<!ELEMENT SPEEDLIMIT (#PCDATA)>
<!ELEMENT CARS (REGNO, ENTRYTIME, EXITTIME)*>
<!ELEMENT REGNO (#PCDATA)>
<!ELEMENT ENTRYTIME (#PCDATA)>
<!ELEMENT EXITTIME (#PCDATA)>
]>

<CARLIST>

<LOCATION> M1 Roadworks north of Luton </LOCATION>
<LENGTH>1</LENGTH>
<SPEEDLIMIT>30</SPEEDLIMIT>

<CARS>

<REGNO>X239FRP</REGNO>
<ENTRYTIME>41000</ENTRYTIME>
<EXITTIME>41060</EXITTIME>

<REGNO>Z239ASW</REGNO>
<ENTRYTIME>41050</ENTRYTIME>
<EXITTIME>41280</EXITTIME>

<REGNO>P389WDE</REGNO>
<ENTRYTIME>42000</ENTRYTIME>
<EXITTIME>42307</EXITTIME>

<REGNO>FE239XTY</REGNO>
<ENTRYTIME>47000</ENTRYTIME>
<EXITTIME>47050</EXITTIME>

</CARS>

</CARLIST>
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

Every time you say "new Speed()", you create a new, separate, individual Speed object. Each Speed object has its own set of the variables you define in that class, and when it's brand new, they're 0, null, etc. So in this SAX handler, you set values into some Speed objects, and never read them, and then you create yet another brand new Speed object and ask it for a report. Of course, the object you ask for a report has never had any of its values set; you set values into other objects, but not this one.

You need to create just one single Speed object in the constructor of your handler, and store it in a member variable of the handler. Then access that one single object in the member everywhere else in your handler -- i.e., don't say "new Speed()" anywhere else but that one place. Then you'll be storing values into just one object, and when you ask that one object for a report later, it will have all the values you set in it available.

Make sense?
 
Anja Schultz
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you.
It does make sense. That's actually what I did first. Unfortunately when I do it like that (I put the statement Speed s = new Speed(); in the main method only) the compiler no longer accepts the methods that start with s. ...

Any ideas?

Best regards

Anja
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're probably missing the part about making "s" a member variable of the handler. So where you have "String elementName;" now, you should put

and then the constructor would be
 
Anja Schultz
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks very much for your help.

I actually had the idea of putting the whole Speed s = new Speed(); at the beginning where I defined all the variables and it worked.

I have also changed how to create the report (using a Hashtable) and now everything is as I want it.

Thanks

Anja
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!