aspose file tools*
The moose likes XML and Related Technologies and the fly likes very simple question which I don't know the answer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "very simple question which I don Watch "very simple question which I don New topic
Author

very simple question which I don't know the answer

Ehsan shin
Greenhorn

Joined: Jun 28, 2011
Posts: 2
Hello, I am new in using SAX and I am trying to use it to read a XML file.
here is my SaxHandler Class which extends DefaultHandler code:

private final class SaxHandlerC extends DefaultHandler {

public void startElement(String uri, String localName,
String qName, Attributes attrs) throws SAXException {
if (qName.equals("S")) {
if (attrs.getValue("s_id").toString().equals(candidateSenId)) {
correctTag = true;
} else {
correctTag = false;
}
}
}

public void characters(char ch[], int start, int length)
throws SAXException {
if (correctTag) {
candidateText = new String(ch, start, length);
correctTag = false;
}

}

public void endElement(String uri, String localName,
String qName)
throws SAXException {

if (qName.equals("S")) {
correctTag = false;
}
}
}


here is my problem: i am reading xml file, everything work fine, but in some cases like this, the characters method is not working correct:
input is:

<S s_id="12">
Nicolaisen said that the SEC investigation of Fannie Mae was continuing.
</S>

but output is:
"Nicolaisen said that the SEC investigation of"

what is the problem? it doesn't make any sense for me.

i would appreciate it if you help me.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
A common trap.

The characters() method is NOT guaranteed to get the full text - it can return as little as a single character because it can only work with the current buffer. Therefore your characters method needs to accumulate text - say in a StringBuilder - until the endElement call.

Bill
Ehsan shin
Greenhorn

Joined: Jun 28, 2011
Posts: 2
Thank you very much.

I used StringBuffer, and now it is working fine.

William Brogden wrote:A common trap.

The characters() method is NOT guaranteed to get the full text - it can return as little as a single character because it can only work with the current buffer. Therefore your characters method needs to accumulate text - say in a StringBuilder - until the endElement call.

Bill
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: very simple question which I don't know the answer