Hi folks, I have a parser parsing out CURRENT_TEMP from an XML file at the moment, but when I use the Stringbuffer() method to handle the temperature to pass it into the variable this.currentTemp I don't think it is working. The reason I think this, is that when I do a test on the this.currentTemp variable like so:
if (this.currentTemp == null) { System.err.println("currentTemp==null"); systemError(); return; }
the program prints out currentTemp==null. Anyways, this is the way I have use the Stringbuffer below:
StringBuffer buffer; String currentTemp;
/************************************************************************** * The following methods are SAX callback routines. Some of them are empty. **************************************************************************/ public void setDocumentLocator(Locator locator) { }
/** * New parsing is started. */ public void startDocument() throws SAXException { System.out.println("at start of the XML file"); this.currentTemp = null; }
public void endDocument() throws SAXException { System.out.println("at end of the XML file"); }
/** * Started processing a new element. If the tag is "ROADSURFACE_TEMP", * prepare a buffer to keep the content. */ public void startElement(String name, Attributes atts) throws SAXException { if (name.equals("ROADSURFACE_TEMP")) { this.buffer = new StringBuffer(); } }
/** * An element is just closed. If the element is "CurrTemp", * copy the content to this.currTemp. */ public void endElement(String name) throws SAXException { if (name.equals("ROADSURFACE_TEMP")) { this.currentTemp = this.buffer.toString(); } }
/** * Accumulate characters if the buffer has been allocated. */ public void characters(char ch[], int start, int length) throws SAXException { if (this.buffer != null) { //this is just checking if the programs getting this far, which it isn't System.out.println("hello there"); this.buffer.append(ch, start, length); System.out.println(this.buffer); } }
Any help is very much appreciated.....Thank you in advance!
One thing may be that you say you are parsing for CURRENT_TEMP tag, but you code is looking for ROADSURFACE_TEMP tags
What I frequently do when doing a sax parse is use a temporary String to hold the contents of character data, then set the string corresponding to the tag with the contents of the temporary String when the parser hits the endElement for that tag. If then else logic...
Sorry, That was a typo, my mistake, I meant ROADSURFACE_TEMP. I'm not sure I fully understand what your saying, am I not already setting up a temporary string in the this.currentTemp variable and passing the value of ROADSURFACE_TEMP into it??