[jdom-interest] setText() to replace children?

Elliotte Rusty Harold elharo at metalab.unc.edu
Thu Jul 12 06:32:58 PDT 2001


At 5:53 PM -0700 7/11/01, Jason Hunter wrote:

>I don't like getText() recursing either because in the data-oriented
>view of XML for which JDOM is optimized, it doesn't make much sense to
>recurse.  I'm happy to have the functionality available somehow, but not
>as the getText() default behavior.
>

But in the data oriented case it doesn't recurse!!!! It does exactly what it's doing now. Making it recurse in the case where there is mixed content will cause no problems or issues for the case where there isn't. 

All we have to do is change this loop:

        while (i.hasNext()) {
            Object obj = i.next();
            if (obj instanceof String) {
                textContent.append((String)obj);
                hasText = true;
            } else if (obj instanceof CDATA) {
                textContent.append(((CDATA)obj).getText());
                hasText = true;
            }
        }

to this loop:

        while (i.hasNext()) {
            Object obj = i.next();
            if (obj instanceof String) {
                textContent.append((String)obj);
                hasText = true;
            } else if (obj instanceof CDATA) {
                textContent.append(((CDATA)obj).getText());
                hasText = true;
            }
            } else if (obj instanceof Element) {
                textContent.append(((Element)obj).getText());
                hasText = true;
            }
        }

The last else if block won't get fired if there are no elements.

While we're at it, we should probably think about what to do if one of the children is an EntityRef since that has the same silent failure problem we have with Element now, though it's probably less damaging in practice. 
-- 

+-----------------------+------------------------+-------------------+
| Elliotte Rusty Harold | elharo at metalab.unc.edu | Writer/Programmer |
+-----------------------+------------------------+-------------------+ 
|          The XML Bible, 2nd Edition (Hungry Minds, 2001)           |
|              http://www.ibiblio.org/xml/books/bible2/              |
|   http://www.amazon.com/exec/obidos/ISBN=0764547607/cafeaulaitA/   |
+----------------------------------+---------------------------------+
|  Read Cafe au Lait for Java News:  http://www.cafeaulait.org/      | 
|  Read Cafe con Leche for XML News: http://www.ibiblio.org/xml/     |
+----------------------------------+---------------------------------+



More information about the jdom-interest mailing list