[jdom-interest] SAXBuilder with regards to the textcontent

Kesav Kumar kesavk at voquette.com
Mon Nov 27 10:00:48 PST 2000

When an element has any text content JDOM behaves very strangly the
mixedcontent gives lots of blank strings and its really difficult to find
out what is the index of the text content.  Why don't we get rid of the
blanstrings insertion into the mixedcontent?  The method public void
characters(char[] ch, int start, int end) dosen't check for the blank
strings etc it blindly adds characters to the mixedcontent.  With this the
mixed content really gets messed with lots of blank string objects.  I feel
it also unnecessarily eats up memory.  One otherthing I noticed is the
getText() method of Element class loops through all the mixedcontent and
constructs the string out of all the string elements why don't we move that
logic into the SAXBuilder before adding to the textcontent?  I guess
getText() method is being called most often which takes time and memory to
loop through all the string elements.  If we move that logic to SAXBuilder
we can get rid of looping every time the getText method being called.  I
modified the void characters like the following

    public void characters(char[] ch, int start, int end)
        throws SAXException {

        String data = new String(ch, start, end);

        if (inCDATA) {
            ((Element)stack.peek()).addContent(new CDATA(data));

         * This is commented out because of some problems with
         *   the inline DTDs that Xerces seems to have.
        } else if (!inDTD) {
            if (inEntity) {
            } else {
                Element e = (Element)stack.peek();
        } else if (inEntity) {
        } else {
            if(data.trim().length() > 0)
                Element e = (Element)stack.peek();
                final List content = e.getMixedContent();
                if ((content == null) || (content.size() < 1) ||
(content.get(0) == null)) {
				if ((content.size() == 1) && (content.get(0)
instanceof String)) {
					String tmp = (String)content.get(0);
					content.set(0, tmp+data.trim());
				int i=0;
                for(final Iterator ite=e.getMixedContent().iterator();
					Object obj = ite.next();
					if(obj instanceof String)
				if(i == content.size())
                else {
					String tmp = (String)content.get(i);
					content.set(i, tmp+data.trim());

Any comments regarding this?

Kesav Kumar
Software Engineer
Voquette, Inc.
650 356 3740
mailto:kesavk at voquette.com

More information about the jdom-interest mailing list