[jdom-interest] dealing with whitespace when appending elements

Jason Hunter jhunter at collab.net
Sun Dec 10 13:26:06 PST 2000


> Great! Except that if I want to preserve the pretty formatting (indentation
> and whitespace), I find the XMLOutputter("  ",true) accumulates the
> whitespace like this:

Here's where XML's mandate that whitespace be preserved gets to be a
serious pain in the rear.  Because the input was nicely formatted you
want to apply *no* formatting to the output lets the additional
formatting mess up the good input.  But... since you added new elements
to the tree, you want those to have whitespace added to format them
nicely.  So you're stuck.  Selecting raw output lets the inputted
elements look good at the expense of the added, but pretty output makes
the added look good at the expense of the inputted.

I think the secret might be to flip the soon-to-be-addedtoggle to remove
surrounding whitespace on input, then do pretty output.  Unless you care
about surrounding whitespace, this will work.  If you do care about
surrounding whitespace, then you need to output raw.

BTW, XMLOutputter has a number of bugs regarding whitespace handling. 
I've asked Alex to deal with them since he's the one who did most of the
coding.  

-jh-



More information about the jdom-interest mailing list