Bug 9495 - Not all element characters being picked up by characters()
Summary: Not all element characters being picked up by characters()
Status: NEW
Alias: None
Product: Crimson
Classification: Unclassified
Component: other (show other bugs)
Version: 1.1.3
Hardware: PC All
: P3 normal with 2 votes (vote)
Target Milestone: ---
Assignee: Edwin Goei
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2002-05-29 15:35 UTC by Gillian Roper
Modified: 2005-03-20 17:06 UTC (History)
0 users



Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Gillian Roper 2002-05-29 15:35:03 UTC
The error is that not all of the characters in an element are being stored in 
the mContent stringbuffer in the characters method e.g. a method coded like 
this:

public void characters(char[] chars, int start, int len)  
                                       throws SAXException {
	    mContent.append( chars, start, len );
            if ( mContent==null ) {
                // Validation takes care of that except for spaces. Discard.
                return;
            } 
}

The error occurs inconsistently but predominately in large XML files. 

To avoid this error for a description element e.g. <description>What a great 
description</description>: 

1. declare a member variable to collect all of the description's characters and 
one to store the current element's name at the beginning of the DefaultHandler
	protected String mCurrentDescription = null;
	protected String mCurrentElementName = null;

2. add the following code to startElement()
	   if ( localName != null && localName.length() > 0 ) {
		    mCurrentElementName = localName;
	    } else if( qName != null && qName.length() > 0 ) {
		    mCurrentElementName = qName;
	    }
	    if( mCurrentElementName == null ) {
		     trace.error( "startElement() no element to handle" );
		     return;
	    }
	    if ( "description".equals( mCurrentElementName ) ){
		    mCurrentDescription = null;
	    }

3. change the characters() to:
public void characters(char[] chars,
                               int start, int len)  throws SAXException
        {
            String charsToAdd = new String( chars, start, len );
	    
	    if ( mCurrentElementName.equals( "description" ) ) {
		    if ( mCurrentDescription == null ){
			    mCurrentDescription = charsToAdd;
		    } else {
			    mCurrentDescription += charsToAdd;
		    }
	    } 
        }

4. reinitialize mCurrentDescription to null in the endElement method.