[jdom-interest] NoSuch*Exceptions in JDOM

Alex Chaffee guru at edamame.stinky.com
Mon Jul 10 11:45:44 PDT 2000


On Mon, Jul 10, 2000 at 01:24:43PM -0400, Elliotte Rusty Harold wrote:
> This isn't a good test. Most of the time an exception won't be 
> thrown!

Says who?

I have code I regularly use for formatting an "item", which contains
the following elements:

title (optional)
url (optional)
email (optional)
location (optional)
date (optional)
description (optional)

The code will output different HTML depending on what's missing.  Most
of the time at least one element is missing, and often 4 out of 6
aren't there.

Right now I need
    String title=null, url=null, date=null, location=null, description=null;
    
    try { title = item.getChild("title").getContent(); }
    catch (NoSuchElementException e) {}
    try { url = item.getChild("url").getContent(); }
    catch (NoSuchElementException e) {}
    try { date = item.getChild("date").getContent(); }
    catch (NoSuchElementException e) {}
    try { location = item.getChild("location").getContent(); }
    catch (NoSuchElementException e) {}
    try { description = item.getChild("description").getContent(); }
    catch (NoSuchElementException e) {}
    
and then I *still need to compare to null later on, in the code that
processes the elements:

    if (title == null && url != null) {
	title = url;
    }
    if (url != null) {
	out.println("<a href='" + url + "'>" + title + "</a><br />");
    }
    else {
	out.println(title + "<br />");
    }
    if (date != null || location != null ) {
	out.println("<font size='-1'><i>");
	
	if (date != null) {
	    out.print(date);
	}
	if (date != null && location != null ) {
	    out.print(" - ");
	}
	if (location != null) {
	    out.print(location);
	}
	out.println("</i></font><br />");
    }

    if (description != null) {
	out.println(description);
	out.println("<br /><br />");
    }


-- 
Alex Chaffee                       mailto:alex at jguru.com
jGuru - Java News and FAQs         http://www.jguru.com/alex/
Creator of Gamelan                 http://www.gamelan.com/
Founder of Purple Technology       http://www.purpletech.com/
Curator of Stinky Art Collective   http://www.stinky.com/



More information about the jdom-interest mailing list