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

Trimmer, Todd todd.trimmer at trizetto.com
Tue Feb 12 13:49:25 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?

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

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.

Todd Trimmer

More information about the jdom-interest mailing list