• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

very simple question which I don't know the answer

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Now I am super curious what sports would be like if we allowed drugs and tiny ads.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic