Problem with DefaultHandler class

Hi all,

While trying to parse data from XML input, I need to build a string to contain the text beween a specific tag and the coma (,) as the seperator.

For instance...

My input:

<SubProjects>

<SubProjectID>2115</SubProjectID>

<ProjectID>631</ProjectID>

<SPNameE>Sept Rel</SPNameE>

<SPNameF>Sept Rel</SPNameF>

</SubProjects>

<SubProjects>

<SubProjectID>2125</SubProjectID>

<ProjectID>637</ProjectID>

<SPNameE>V5.41</SPNameE>

<SPNameF>V5.41</SPNameF>

</SubProjects>

<SubProjects>

<SubProjectID>2145</SubProjectID>

<ProjectID>706</ProjectID>

<SPNameE>V1.11</SPNameE>

<SPNameF>V1.11</SPNameF>

</SubProjects>

<SubProjects>

<SubProjectID>2146</SubProjectID>

<ProjectID>631</ProjectID>

<SPNameE>V8.0</SPNameE>

<SPNameF>V8.0</SPNameF>

</SubProjects>

......

and elementToSearchFor=subProjectID, my ouput SHOULD BE a string such as

2115, 2125, 2145, 2146

It is working fine for a while. Now I got the problem, the output for the input above is

2115, 2125,214, 2146

I check for the length in the characters() method, the length for the input 2145 is just 3. That's why it just returns 214, rather than 2145. The item that has the problem is in the middle of the input (not first, not last) and it is OK with some other inputs. That means it will have problem at a certain condition. I really don't know why/when. Please help! Thanks so much...

Here is my codes

public class FtsSAXHandler extends DefaultHandler {

private String result = "";

private String elementToSearchFor = "";

private boolean foundResult = false;

public FtsSAXHandler () {

}

public String getResult () {

return result;

}

public void setElementToSearchFor (String elementName) {

elementToSearchFor = elementName;

}

public String getElementToSearchFor () {

return elementToSearchFor;

}

public void startElement (String namespaceURI,

String localName,

String qName,

Attributes attributes) throws SAXException {

if (foundResult == false) {

foundResult = (localName.compareTo (elementToSearchFor) == 0);

}

}

public void characters (char[] characters, int start, int length) throws SAXException {

if (foundResult == true) {

if (result != "" ) {

result = result + "," + String.valueOf(characters, start, length);

}

else {

result = result + String.valueOf(characters, start, length);

}

foundResult = false;

}

}

}

[2989 byte] By [MaggiePa] at [2007-10-2 22:24:20]
# 1

The characters() method is not guaranteed to provide the entire text node all at once. The parser is permitted to break a text node into pieces and call characters() several times, once with each piece. That is what is happening to you.

So: in startElement() create a new StringBuffer. In characters() append the chars to the StringBuffer, using the append(char[], int, int) method. Use the contents of the StringBuffer in the endElement() method.

DrClapa at 2007-7-14 1:41:43 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2

You may consider doing the following:

public class FtsSAXHandler extends DefaultHandler {

private CharArrayWriter contents = new CharArrayWriter();

private Vector subProjectIds=new Vector();

public void characters( char[] ch, int start, int length )

throws SAXException {

contents.write( ch, start, length);

}

public void startElement(String namespaceURI, String localName, String qName,

Attributes attr ) throws SAXException {

contents.reset();

}

public void endElement( String namespaceURI, String localName, String qName)

throws SAXException {

if ( localName.equalsIgnoreCase(SubProjectID) {

subProjectIds.add(contents.toString().trim());

}

}

}

This is just a guideline and I haven't tested this in my machine.

debajyotibanerjeea at 2007-7-14 1:41:43 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 3
Hi all,Thanks for your prompt reply. It helps me to solve the problem.
MaggiePa at 2007-7-14 1:41:43 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...