RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Java
Expertise: Beginner
May 24, 2005

Read the Entire Text Element

SAXParser's DefaultHandler provides a callback for public void characters(public char[] ch, int start, int length, etc.) and throws a SAXException for reading element text contents. You can read the char array in any invocation of the DefaultHandler and convert it into a String object for your use. However, this single character() call is not sufficient to read entire element text.

Luckily, SAXParser's specifications allow parsers to split text data into multiple arrays and call characters multiple times. A better method would be to:

  1. Create a String or StringBuffer object in the startElement method:
    public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws
    SAXException {
         m_elemText	= new StringBuffer();
  2. Append the text into the String or StringBuffer for each characters call:
    public void characters(char[] ch, int start, int length) throws SAXException {
        m_elemText.append(new String(ch, start, length) );
  3. In endElement, read the entire element text:
      public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
              System.out.println(Element text: " + m_elemText.toString());
Complying with this SAX specification requirement can save you hours of debugging when you hit this limitation in a production environment.
Amit Jain
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date