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

Jon Baer jonbaer at digitalanywhere.com
Wed Feb 13 03:15:53 PST 2002


Seems like this was also already on TODO:

 Consider Mike Jennings' proposal of two new methods on Element:
  public String getAttributeValue(String name, String default)
  public String getAttributeValue(String name, Namespace ns, String default)
  http://lists.denveronline.net/lists/jdom-interest/2000-December/004155.html

I like the default idea.

- Jon

Bart Read wrote:

> > > 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.
> >
>
> Actually I really like this idea.  Sure, one side effect of this is that is
> could "mask" errors, but really no more so than for example the
> getProperty(String key, String defaultValue) method of the
> java.util.Properties class.  One situation where it WOULD be very useful is
> if you have an attribute that should have a default value -- you could
> assign it without having to either (i) turn on validation, or (ii) do
> something like:
>
> String value = element.getAttributeValue("myAttribute");
> value = (value != null ? value : "default");
>
> which is a maybe a wee bit cumbersome.  Certainly, I'd be in favour of
> re-introducing this method.  However, what I definitely DON'T want to do is
> spark off a huge debate over an issue that's already been discussed in
> considerable depth, and which many people are happy about, so if the
> consensus is "no" then I'll happily go with that.
>
> Cheers,
> Bart Read
>
> ---------- Automatically inserted by WDI Mailserver ----------------
> This message has been scanned for virus, trojans, illegal relays and
> spam. If you have any comments, problems or questions please do not
> hesitate to contact:
> Abbotsbury Software Ltd
> Tel: +44 1305 871543 Fax: +44 1305 871688
> Email: postmaster at wdi.co.uk
> Web: http://www.wdi.co.uk/virus.phtml
> _______________________________________________
> To control your jdom-interest membership:
> http://lists.denveronline.net/mailman/options/jdom-interest/youraddr@yourhost.com




More information about the jdom-interest mailing list