[jdom-interest] attribute handling query

Anwar, Naveed Naveed.Anwar at gs.com
Mon Jul 3 08:53:46 PDT 2000


It may be nice to have the second solution, that getAttribute(String) should
return null if attribute doesn't exist. getChild/getChildern might want to
behave the same way.

Thanks
-Naveed

-----Original Message-----
From: Petri Wessman [mailto:orava at termiitti.akumiitti.fi]
Sent: Monday, July 03, 2000 8:36 AM
To: jdom-interest at jdom.org
Subject: [jdom-interest] attribute handling query


I'm sorry if this has been covered on this list previously, I've been
a bit too busy to actually read the list for a while :/

In any case, I have a small enhancement request for the Element
attribute retrieval methods. Currently, the getAttribute(name) method
throws an exception if the attribute does not exist. This is quite
inconvenient, as I have a lot of XML which has optional attributes,
and putting a try{ } catch () block around all the attribute handling
code is both messy, and against the general design rule that
exceptions should not be encountered during the normal operation of an
application.

In other words, currently I have to do something like this:

  String attr = null
  try {
      attr = elem.getAttribute().getValue();
  } catch (NoSuchAttributeException e) {
      // ignore
  }


I would very much like to see either something like the following in
Element:

  Attribute getAttribute(String name, boolean missingOk)

  (returns null is missingOk == true and attribute is missing)

or

  boolean hasAttribute (String name)

  (so I could at least query whether the attribute is there and avoid
  triggering an exception).

If this capability already exists and I've missing something, please
enlighten me. JDOM is great otherwise, and saves a ton of memory
compared to storing normal DOM trees. Keep up the good work :)

P.S. I know that I could get the attribute List and then examine that
to see whether the attribute exists, but that seems like a waste of
resources (useless List instantiation etc).

//Petri
_______________________________________________
To control your jdom-interest membership:
http://lists.denveronline.net/mailman/options/jdom-interest/youraddr@yourhos
t.com



More information about the jdom-interest mailing list