[jdom-interest] How to ease traversal of JDOM tree

Laurent Bihanic laurent.bihanic at atosorigin.com
Tue Nov 20 08:56:43 PST 2001


Alex Rosen wrote:

> Can you provide a real-world example of when you'd want to alter a node
> without knowing its type? If you have to check the node's type anyway, then
> I suggest that having these methods on the interface isn't providing much
> added value.
> 

Most of the time, I don't want to alter a node without knowing its type, I 
just want to be able to add content to a node without knowing the content type.
Now, sometimes, it'd nice to move some nodes from an element to a document, 
from a document to an element, from an element to another element or from a 
document to another document without coding 4 different simple methods.
(Yes I know, PartialList can be used solve this specific example.)

Basically, I want to be able to provide a single method to alter a node. 
Today, I have to make the argument of this method to be an Object and 
implement something like:
  if (o instanceof Element) { target.addContent((Element)o); }
  if (o instanceof Comment) { target.addContent((Comment)o); }
  if (o instanceof ProcessingInstruction) { 
target.addContent((ProcessingInstruction)o); }
  ...

Sometimes, I bypass the problem by using a List I append to the PartialList 
returned by Element.getContent().

Laurent




More information about the jdom-interest mailing list