[jdom-interest] instanceof Text and XMLOutputter request

toddobryan at mac.com toddobryan at mac.com
Wed Apr 3 15:02:36 PST 2002

Yowzers. I just found a terrible, insidious little bug in a program that 
used to work because Text objects are now returned instead of String 
objects from Element.getContent().

Okay, I just had to complain before getting on to the real point of this.

I need a String representation of the content in an Element. If I were 
outputting said String, I could use XMLOutputter.outputElementContent(), 
but I'm not and there is no equivalent outputString method. So that 
people could avoid writing instanceof lines that could break in a future 
version, how would people feel about the following possible methods in 

(a) String outputElementContentAsString(Element e)


(b) void output(Object o)
       void ouputString(Object o)

where these take any JDOM Object (currently CDATA, Element, Text, etc.) 
and do the right thing. In other words, you wouldn't have to check 
instanceof's and cast each member that you get in a List from 
getContent() prior to outputting it. If more object types are added to 
the API, your code would not break, and if for some reason you try to 
XMLOutput non-JDOM objects, the default behavior could be just to output 
their toString() value.

Heck, if you're worried about making the API messy, just take out all 
the particular versions of the methods above...


More information about the jdom-interest mailing list