[jdom-interest] Abstracting interface to JDOM nodes.

Elliotte Rusty Harold elharo at metalab.unc.edu
Sat Aug 26 11:06:03 PDT 2000

At 10:08 AM -0600 8/26/00, Trebor A. Rude wrote:

>	Strange, the more recent versions in the CVS have been 
>getting closer to
>completing the getSerializedForm() functionality. It certainly would have
>been easier for what I wanted to use it for (getting the minimalistic XML
>representation for passing across CORBA). What's the proper usage of the
>XMLOutputter for that use case, something like this:
>XMLOutputter xmlWriter = new XMLOutputter("", false);
>ByteArrayOutputStream byteRep = new ByteArrayOutputStream();
>xmlWriter.output(someDocument, byteRep);
>String stringRep = byteRep.toString();

It will be simpler and more or less look like this:
XMLOutputter xmlWriter = new XMLOutputter("", false);
String s1 = xmlWriter.serialize(document);
String s2 = xmlWriter.serialize(element);
String s3 = xmlWriter.serialize(attribute);

However, that functionality isn't there yet. Currently, I've stopped 
working on this part of XMLOutputter until we can resolve the 
question of where to put the configuration options. Also, before 
anybody does anything else with XMLOutputter I'd like to get the new 
Namespace code and fixes rolled in.

>>2. You don't need a common interface. instanceof works just fine for
>>this use case.
>	So I'd have to have a separate instanceof test for all the different
>types of nodes? That seems terribly inelegant to me. Interfaces were
>designed to prevent just that kind of thing.

It doesn't seem all that inelegant to me, and I disagree that 
interfaces were designed to handle this case. Interfaces were 
designed to handle the case when you genuinely do not know in advance 
what types you might deal with. Here we have a very finite, small 
list of possible types that will not change.  The reason there's no 
Node interface in JDOM is two-fold:

1. Strings are used instead of Text nodes and we can't change the String class.

2. There simply isn't that much common functionality shared between 
the different node types. For instance, only Document and Element 
have children so you shouldn't put a getChildren() method in a Node 

| Elliotte Rusty Harold | elharo at metalab.unc.edu | Writer/Programmer |
|                  The XML Bible (IDG Books, 1999)                   |
|              http://metalab.unc.edu/xml/books/bible/               |
|   http://www.amazon.com/exec/obidos/ISBN=0764532367/cafeaulaitA/   |
|  Read Cafe au Lait for Java News:  http://metalab.unc.edu/javafaq/ |
|  Read Cafe con Leche for XML News: http://metalab.unc.edu/xml/     |

More information about the jdom-interest mailing list