[jdom-interest] XMLOutputter to the rescue (get/setText)

Alex Chaffee guru at stinky.com
Wed Jul 11 12:03:37 PDT 2001


Wow, getText and setText are feisty little critters, huh?

If
  getText does not recurse
  setText replaces the entire content

then they'll

(a) be efficient and predictable
(b) satisfy the needs of those who use XML as a glorified properties file 
(name=value -> <name>value</name>).

However, for the XML-as-document folks, for whom elements can contain mixed 
text and elements with their own text etc., like

<warning>
  Do <b>not</b> eat the <font color="#FFFF00">yellow</font> snow.
</warning>

I propose using (recommending to use) XMLOutputter.

XMLOutputter already works for the case where you want to preserve the tags:

new XMLOutputter().outputElementContent(element, out)

will return "\n Do <b>not</b> eat the <font color="#FFFF00">yellow</font> 
snow.\n" which is probably what many people will really want anyway -- it's 
suitable for sticking in a JSP or whatever.

If you *don't* want to preserve the tags, I can add a property to 
XMLOutputter, omitTags, that will handle that.  Coupled with the intelligent 
whitespace handling that's already mostly working, and which I promise to 
get wholly working as soon as my latest patch is accepted, it will mean

XMLOutputter textonly = new XMLOutputter();
textonly.setOmitTags(true);
textonly.setTextNormalize(true);
textonly.outputElementContent(element, out);

gives

"Do not eat the yellow snow."

which is probably what the other half of mixed contenteers want.

No need to overload the semantics of Element to become a utility for 
outputting a variety of text formats -- that's what XMLOutputter is for.

(We could also make a "TextOutputter" that could be a bit more efficient at 
doing the job, but I think XMLOutputter can handle it.)

  - A

-- 
Alex Chaffee                       mailto:alex at jguru.com
jGuru - Java News and FAQs         http://www.jguru.com/alex/
Creator of Gamelan                 http://www.gamelan.com/
Founder of Purple Technology       http://www.purpletech.com/
Curator of Stinky Art Collective   http://www.stinky.com/




More information about the jdom-interest mailing list