[jdom-interest] XMLOutputter to the rescue (get/setText)

Elliotte Rusty Harold elharo at metalab.unc.edu
Wed Jul 11 14:21:22 PDT 2001


At 12:03 PM -0700 7/11/01, Alex Chaffee wrote:
>Wow, getText and setText are feisty little critters, huh?
>
>If
> getText does not recurse
> setText replaces the entire content
>
>then they'll
>
>(a) be efficient and predictable
>(b) satisfy the needs of those who use XML as a glorified properties file (name=value -> <name>value</name>).
>

However, if

  getText DOES recurse
 setText replaces the entire content

then they'll

(a) be efficient and predictable
(b) satisfy the needs of those who use XML as a glorified properties file (name=value -> <name>value</name>).

I really don't see what's gained by making them not recursive. It's not like any more work has to be done in the case where there is no mixed content. 

>However, for the XML-as-document folks, for whom elements can contain mixed text and elements with their own text etc., like
>
><warning>
> Do <b>not</b> eat the <font color="#FFFF00">yellow</font> snow.
></warning>
>
>I propose using (recommending to use) XMLOutputter.
>

No, a recommendation is not a solution. We should not provide methods that can fail without warning in such a common use case. I think there are three acceptable solutions. In order of preference, 

1. We recurse the text so the method always works.
2. We throw an exception in the face of mixed content.
3. We delete the getText() method completely.

The current behavior is simply not an option, even with a Javadoc note on the point. It's just way too dangerous. 


-- 

+-----------------------+------------------------+-------------------+
| 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