[jdom-interest] Re: First impressions and some suggestions

Elliotte Rusty Harold elharo at metalab.unc.edu
Sat Jun 10 06:39:42 PDT 2000

At 1:51 PM -0700 6/9/00, Jason Hunter wrote:
>> Or how about this case:
>> <name><first>Alex</first><last>Chaffee</last></name>
>> Should JDOM insert extra white space between "Alex" and "Chaffee"
>> because that's what most people would initially expect?
>Elliotte, you're losing me here.  The code is this.
>String first = name.getChild("first").getContent();
>String last = name.getChild("last").getContent();

No, the problem code is this. What does

String fullName = name.getContent();

return? The "obvious" answer is "Alex Chaffee"; though that's not 
what JDOM is doing now or what it should do. My point is that the 
obvious answer isn't always the correct answer.

>The question is does
>  <first>
>     Alex
>  </first>
>  <last>
>    Chaffee
>  </last>
>behave the same, or not, for the common case of calling getContent().  I
>think it should because that's what developers expect, and
>getContent(true) can work for those relatively rare cases where you want
>the whitespace.

I don't think these cases are rare. I think they're much more common 
than you expect.

>We are still fully spec compliant.  We just have two calls, one that
>returns surrounding whitespace and one that doesn't.  The one that
>doesn't is less typing because it's more common.  After all, the spec
>says, "Such white space is typically not intended for inclusion in the
>delivered version of the document" and this is a Java-based document
>object model.

I disagree that this API is fully spec compliant. I think you're 
telling me that you feel the XML spec is wrong; that white space 
should be less significant than XML 1.0 actually makes it. That 
argument I can see. But I think that even if the XML 1.0 inventors 
made the wrong decision on this point, it's more important to adhere 
to the spec than to try to fix its shortcomings.  The content of an 
element includes white space according to XML 1.0. getContent() 
should return what the XML 1.0 spec says the content is; not some 
bowdlerized construct that's sort of like the content but not quite. 
If you want to provide a utility method so developers don't have to 
call trim(), then that's the one that should have a different name, 
something like getTrimmedContent() or getNormalizedContent().

| Elliotte Rusty Harold | elharo at metalab.unc.edu | Writer/Programmer |
|                  The XML Bible (IDG Books, 1999)                   |
|              http://metalab.unc.edu/xml/books/bible/               |
|   http://www.amazon.com/exec/obidos/ISBN=0764532367/cafeaulaitA/   |
|  Read Cafe au Lait for Java News:  http://metalab.unc.edu/javafaq/ |
|  Read Cafe con Leche for XML News: http://metalab.unc.edu/xml/     |

More information about the jdom-interest mailing list