Read the Entire Text Element

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) throwsSAXException {     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.

See also  Essential Measures for Safeguarding Your Digital Data

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist