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;
}
}
}

