[jdom-interest] Recursively Modifying the JDOM before output.

Alex Rosen arosen at silverstream.com
Fri Jun 1 14:00:09 PDT 2001

Jason wrote:
> We could also just put the text before or after the other
> content?  That
> might be less ugly.  Perhaps we could be fancy (too fancy?) and set it
> at the first non-whitespace text node.

I think that if your element has mixed content, then you shouldn't be using
setText() (you should use setMixedContent(), or modify the return value of
getMixedContent(), instead). Trying to be fancy will be counterproductive - it
might lull people into thinking that setText() is OK to use in this situation.
It probably is bad to silently drop the children, though, so I like your first

Gary Montgomery wrote:
> So we save <kid/> off to the side with getChildren().
> setText("foo") changes the tree to:
> <root>
> foo
> </root>
> then after addChildren():
> <root>
> foo
> <kid/>
> </root>

Watch your whitespace... what you'd actually get is:


and then:


(Normally I'd apologize about being too pedantic, but unfortunately XML kind of
forces you to be anal about whitespace - it causes all kinds of problems. If
you can change things around so that you don't have mixed content (i.e. both
text and child elements under the same parent), you'll be a lot happier...)


More information about the jdom-interest mailing list