[jdom-interest] The JDOM Model, Chapter 15 of Processing XML with Java

Alex Rosen arosen at silverstream.com
Tue May 7 08:24:20 PDT 2002


> >Opposite to XPath, JDOM does not define what the value of a node is.
> >But is this really a problem? XPath does not define a general rule,
> >it simply defines what the text value is for each type of node.
> >We could do the same thing with JDOM (i.e. add a public String
> >getValue()/getText() method to each node or to the Node interface)
> >but we would still hav e to define what thereturned value is on a
> >per-node basis.
> >
>
> I've suggested simply adopting the XPath rules here. The biggest
> problem with not having this method is that it's really not possible
> to easily get the string content of an element.
>
> >But I'm not sure this would really be useful except for implementing
> >XPath engines.
> >
>
> It's useful anytime someone wants the string content of an element.
> As currently implemented getText() is dangerous. It can lose content
> silently, unexpectedly, and with no warning.

Interesting, I kind of have the opposite view from you (and unfortunately
from the XPath people). If you're using a document that has no mixed
content, then the getText() rules (no recursion) and the XPath rules
(recursion) are identical. If your document may have mixed content, then the
XPath rule is useful 99% of the time but dangerous 1% of the time. Dangerous
meaning that it can lose information silently, unexpectedly, and with no
warning. See
http://www.servlets.com/archive/servlet/ReadMsg?msgId=118234&listName=jdom-i
nterest

I like getText() because I can often know for sure if I'm using a
no-mixed-content document, and then I know getText() is safe. If I don't
know this, I shouldn't use it. With the XPath rules, it encourages me to ask
for the string value of a mixed-content element, which I think is dangerous.
A getXPathStringValue() type method would be more tempting to use in this
situation than the current getText(), which is why I don't like it.

Of course, the XPath people disagree with me, presumably with good reason,
so that may be reason enough to use their rules.

Alex




More information about the jdom-interest mailing list