[jdom-interest] setText() to replace children?

Alex Rosen arosen at silverstream.com
Thu Jul 12 08:19:06 PDT 2001


> <p>
>   Do <strong>NOT</strong> eat the yellow snow!
> </p>

Or how about this one: "<p>Thou shalt <strong>NOT</strong> commit adultery.<p>"
:)

On the other hand, consider this footnote: "<p>The dosage is 3<sup>2</sup>
milligrams.</p>". (<sup> means superscript.) Now you've just killed the
patient. :)

The problem is this: IN THE GENERAL CASE, IT IS NOT POSSIBLE TO CREATE AN
ADEQUATE PLAIN-TEXT REPRESENTATION OF AN XHTML ELEMENT. (Or other
document-oriented formats, presumably.) When tags like <ol>, <table>, <sup>,
<strike>, etc. can be appear anywhere in the text, simply concatenating the
text pieces can be a dangerous a thing to do.

You're right, making getText() recurse will make this kind of thing work better
a lot of the time, but I see that as a bad thing - it'll still work incorrectly
(or at least sub-optimally) some percentage of the time, and the fact that it
makes things seem OK more of the time is a step backwards. If you're using
mixed content, you at least need to *think* about the fact that there might be
meaningful tags there, rather than having us skip them for you. I'm confident
that with adequate testing, JDOM users will notice the problems that the
current getText() can cause. I'm not confident that then will notice the
problems that your proposed getText() will cause, since they will occur much
less often.

> No hidden errors!

No absolute rules! Seriously, we've been backed into a corner by XML. It has
two equally valid uses, whose requirements are fairly different, and JDOM is
trying to make both camps happy. Throwing a checked exception here would really
piss off the other half of our users, since they can often know beforehand that
there are no child elements (because they built their XML programmatically, or
they parsed it using validation). I could live with an unchecked exception, but
it just seems... heavy-handed.

Alex Rosen
SilverStream Software




More information about the jdom-interest mailing list