[jdom-interest] Bug in CDATA reading/writing?

Jason Hunter jhunter at collab.net
Thu Mar 22 10:27:10 PST 2001

Mark Roder wrote:
> Thanks for checking in the new XMLOutputter.java(CVS rev 1.41)
> I am noticing a problem that you/someone may be able to track down better
> than I can.
> If I construct a outputter with:
> XMLOutputter xmlOut = new XMLOutputter("  ",true);
> And read and then write out a file that contains:
> <Root>
> <ValueOne>This &amp; That</ValueOne>
> <ValueTwo>This <![CDATA[&]]> That</ValueTwo>
> <LongElem><Tag1>T1</Tag1><Tag2><Tag21>T21</Tag21></Tag2><Tag3>T3</Tag3></Lon
> gElem>
> </Root>
> The ValueOne, ValueTwo and LongElem are not indented, but the line before
> them has 2 spaces on it and a newline.  Notice it goes
> <Root>\r\n<space><space>\n<ValueOne>.

That's proper behavior.  Remember, all whitespace from the original file
is maintained (per the XML spec) and you just asked the outputter to add
an *extra* indent and some *additional* new lines.  Since there's
already a lot of new lines in the file, the output isn't terribly pretty
because of the mixing of original and added new lines.  Those output
settings are more appropriate if you built the entire document in memory
and there's no pre-existing whitespace for formatting.  With a doc from
a file you'll want to setTrimText(true) to see pretty output because
then the outputter can do away with pre-existing whitespace and only add
its own.  See the Javadocs where I described the settings for various
kinds of output.  The output then is:

<?xml version="1.0" encoding="UTF-8"?>
  <ValueOne>This &amp; That</ValueOne>
  <ValueTwo>This <![CDATA[&]]> That</ValueTwo>


The empty line toward the bottom I'd rather not have there, but it's a
real pain to get rid of (I'll explain the details sometime later if
anyone wants to help fix it), and before worrying about it I wanted to
make sure everything else was OK.


More information about the jdom-interest mailing list