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

Jason Hunter jhunter at servlets.com
Wed Feb 13 01:26:05 PST 2002

> 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


It's "".

What is the text of


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?


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.


More information about the jdom-interest mailing list