[jdom-interest] Hard-Coded Newlines

Jason Hunter jhunter at collab.net
Thu Feb 22 11:07:06 PST 2001

Lincoln.J.Carlton at WellsFargo.COM wrote:
> I am creating a string of XML from a Document using the XMLOutputter class.
> I have set newlines false, yet see two newlines in my output string, one
> after the declaration and one at the end of the xml-string.

OK, this was added between b5 and b6.  The reason for it is that if you
want to "round trip a document" meaning write a doc as close as possible
to the input document, you want to read keeping all whitespace and write
adding no additional whitespace.  This works pretty well because all
whitespace within the element structure is unchanged.  But (as per an
earlier email this morning) whitespace outside the element structure
gets thrown away by SAX on read.  So if you read a doc and write it
without adding whitespace you end up with the root element on the first
line along with the declaration, and your round tripping is ruined.  We
can't know exactly how the whitespace outside the element structure
looks, but we guess that you didn't put the root element on the first
line and thus add a new line.  :-)  It's not a great solution for the
reason it caused you to be confused, but there are no good solutions
when it comes to XML whitespace.  I'm glad that by setting the line
separator to "" you can still get what you want.  I'm open to
suggestions for a better approach.


More information about the jdom-interest mailing list