It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Board index Programming Java Forum

Problems with variables when accessing XML-file with Java

Problems with variables when accessing XML-file with Java

Postby asdaanaa on Sat Feb 03, 2007 1:21 am

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>
asdaanaa
 
Posts: 0
Joined: Sat Feb 03, 2007 1:14 am

Who is online

Users browsing this forum: No registered users and 0 guests