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.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

Top 5 B2B SaaS Marketing Agencies for 2023

In recent years, the software-as-a-service (SaaS) sector has experienced exponential growth as more and more companies choose cloud-based solutions. Any SaaS company hoping to stay ahead of the curve in this quickly changing industry needs to invest in effective marketing. So selecting the best marketing agency can mean the difference

technology leadership

Why the World Needs More Technology Leadership

As a fact, technology has touched every single aspect of our lives. And there are some technology giants in today’s world which have been frequently opined to have a strong influence on recent overall technological influence. Moreover, those tech giants have popular technology leaders leading the companies toward achieving greatness.

iOS app development

The Future of iOS App Development: Trends to Watch

When it launched in 2008, the Apple App Store only had 500 apps available. By the first quarter of 2022, the store had about 2.18 million iOS-exclusive apps. Average monthly app releases for the platform reached 34,000 in the first half of 2022, indicating rapid growth in iOS app development.