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

Elliotte Rusty Harold elharo at metalab.unc.edu
Tue Jul 10 19:28:20 PDT 2001

At 12:55 PM -0500 7/10/01, dfeather at oreillyauto.com wrote:

>I think the setContent(XXX xxx) methods make alot of sense. I think it 
>would be a mistake, and kind of annoying to have just setContent methods 
>for a List and a String. If you want to take a minimalist approach to the 
>question, then why have one specifically for just String? A String is just 
>another object, so we might as well do it all with just the List method 
>and add the String to the List then add the List.
>In my opinion, the many setContent methods would make the API much easier 
>to use. If I want to add an Element, I don't need to know that I have to 
>add it to a List and then setContent, I can just setContent. The same 
>would go for String and everything else, all with one polymorphic method. 

I think you just proved my point. The setContent(Element) method would not do what you seem to think it would do. It would remove all other content from the parent element and replace it with the single element. It would not *add* an element. There's already a whole slew of addContent() methods to do what you want to do. Those do make sense, because there's a lot of reason to want to add processing instructions and comments and such to elements. But when's the last time you saw an element like this?

<foo><?myPI mi PI data?><?foo>

or like this?

<foo><!-- a rather small comment --><?foo>

That's what the additional setContent() methods would do. 

You do see elements like this:


which is why setContent(String text) makes sense. Overloading for the sake of overloading is not a virtue. 

| 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