Problems with Java XML-access

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>

[5358 byte] By [asdaanaaa] at [2007-11-26 17:10:01]
# 1

When you post code here, please wrap it in [code][/code] tags so it's legible.

If you're using a parser that's not included in the standard JDK, you might want to ask your question on a forum specific to whoever provided it. If you're using an IBM-distributed version of SAX maybe you should ask on an IBM forum.

Anyway, this:

public void error(SAXParseException se){

//No code needed here

}

public void warning(SAXParseException se){

//No code needed here

}

is, in my opinion, bad news. Presumably the parser invokes these when there's an error. Since things aren't working, it's not a bad assumption that there may be errors. You shouldn't ignore them.

At least throw some output statements in there, like System.err.println("Error: " + se).

paulcwa at 2007-7-8 23:37:50 > top of Java-index,Java Essentials,Java Programming...