user755806
user755806

Reputation: 6815

SAX Parser characters method doesn't collect all content

I'm using SAX parser to parse XML and is working fine.

I have below tag in XML.

<value>•CERTASS >> Certass</value>

Here I expect '•CERTASS >> Certass' as output. but below code returns only Certass. Is there any issue with the special chars of value tag?

public void characters(char[] buffer, int start, int length) {
           temp = new String(buffer, start, length);
    }

Upvotes: 6

Views: 3805

Answers (2)

fl0w
fl0w

Reputation: 3877

I ran into this problem the other day, it turns out the reason for this is the CHaracters method is being called multiple times in case any of these Characters are contained in the Value:

"   &quot;
'   &apos;
<   &lt;
>   &gt;
&   &amp;

Also be careful about Linebreaks / newlines within the value!!! If the xml is linewrapped without your controll the characters method wil also be called for each line that is in the statement, plus it will return the linebreak! (which you manually need to strip out in turn).

A sample Handler taking care of all these problems is this one:

 DefaultHandler handler = new DefaultHandler() {
   private boolean isInANameTag = false;
   private String localname;
   private StringBuilder elementContent;

   @Override
   public void startElement(String uri, String localName,String qName, Attributes attributes) throws SAXException {
    if (qname.equalsIgnoreCase("myfield")) {
      isInMyTag = true;
      this.localname = localname;
      this.elementContent = new StringBuilder();
    }
   }

   public void characters(char[] buffer, int start, int length) {
      if (isInMyTag) {
         String content = new String(ch, start, length);
         if (StringUtils.equals(content.substring(0, 1), "\n")) {
              // remove leading newline
              elementContent.append(content.substring(1));
         } else {
              elementContent.append(content);
         }
      }
   }

   public void endElement(String uri, String localName, String qName) throws SAXException {
     if (qname.equalsIgnoreCase("myfield")) {
       isInMyTag = false;
       // do something with elementContent.toString());
       System.out.println(elementContent.toString());
       this.localname = "";
     }
   }
}

Upvotes: 0

Rudi Kershaw
Rudi Kershaw

Reputation: 12982

It is not guaranteed that the characters() method will run only once inside an element.

If you are storing the content in a String, and the characters() method happens to run twice, you will only get the content from the second run. The second time that the characters method runs it will overwrite the contents of your temp variable that was stored from the first time.

To remedy this, use a StringBuilder and append() the contents in characters() and then process the contents in endElement(). For example:

 DefaultHandler handler = new DefaultHandler() {
     private StringBuilder stringBuilder;

     @Override
     public void startElement(String uri, String localName,String qName, Attributes attributes) throws SAXException {
         stringBuilder = new StringBuilder();
     }

     public void characters(char[] buffer, int start, int length) {
         stringBuilder.append(new String(buffer, start, length));
     }

     public void endElement(String uri, String localName, String qName) throws SAXException {
         System.out.println(stringBuilder.toString());
     }
 };

Parsing the String "<value>•CERTASS >> Certass</value>" and the handler above gives the output:

?CERTASS >> Certass

I hope this helps.

Upvotes: 9

Related Questions