[jdom-interest] attribute handling query
Petri Wessman
orava at termiitti.akumiitti.fi
Mon Jul 3 05:35:39 PDT 2000
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
More information about the jdom-interest
mailing list