[jdom-interest] getText() and getAttributeValue()

philip.nelson at omniresources.com philip.nelson at omniresources.com
Wed Feb 13 06:11:23 PST 2002


 This is exactly right.  At one time there was a project that Alex had
started to move the default value issue to a helper class.  Even that got
too much as all the variations on the right helpers started to add up.

I think the best answer is, it can be done with a helper class, it can be
done in a subclass and it can be done very easily in the calling code, so it
doesn't need to be addressed by JDOM right now.

-----Original Message-----
From: Jason Hunter
To: Trimmer, Todd
Cc: 'jdom-interest at jdom.org'
Sent: 2/13/02 3:26 AM
Subject: Re: [jdom-interest] getText() and getAttributeValue()

> For Element, if no textual content exists, getText() returns an empty
> string. However, if getAttributeValue() is called for an attribute
that does
> not exist, null is returned. Why the inconsistency?

What is the text of

<foo></foo>

It's "".

What is the text of

<foo/>

It's "" also because it's semantically identical to <foo></foo>.

Now, what is the text of this attrib value?

<foo attrib="">

It's clearly "".

What is the text of this attrib value?

<foo/>

There is none, so it's null.

> Under what circumstances would client code care to differentiate
between an
> attribute existing with empty string and the attibute not existing at
all?

I'm sure there are many cases.

> getAttributeValue() should return empty string if the attribute does
not
> exist because:
> 1) It makes it more consistent with getText()
> 2) One won't have to check for null on the return value.
> 
> Right now, my code is littered with:
> String name = Parse.assure(elem.getAttributeValue("name"));
> String id = Parse.assure(elem.getAttributeValue("id"));
> // etc.
> 
> ...where Parse.assure(String str) returns empty string if str is null;
or
> str unchanged if it is non-null. It sure would be nice to get rid of
all
> those checks.

What you'd really want is getAttributeValue(String name, String
fallback).  It's even more powerful since "" doesn't have to be the
fallback.  We had that before but it was removed since people didn't
like its ability to "mask errors".  I personally would rather have it.

-jh-
_______________________________________________
To control your jdom-interest membership:
http://lists.denveronline.net/mailman/options/jdom-interest/youraddr@you
rhost.com



More information about the jdom-interest mailing list