[XOM-interest] A small request

dirk bergstrom dirk at juniper.net
Tue Jan 6 18:41:59 EST 2004

I recently discovered that I need to preserve whitespace in my xml config
files.  I dutifully added the following to my code (I like to be explicit):


And everything seemed right in the world.

That is, until I needed to diff a couple of files, and the diffs were
*huge*.  A ten character change made for a diff bigger than the file,
because the whole file was one long line.

I appreciate that whitespace has been scrupulously preserved, but the
document is no longer really human-readable.

My documents have no mixed content -- elements contain either text, or other
elements, but never both.  This means that adding a linebreak after each
close tag restores readability (and diff-ability).

I suspect that a large number of users will find themselves in a similar
situation to mine, needing to preserve whitespace inside tags, while keeping
the overall document legible.  It would be nice to have a convenience method
for this, something along the lines of setBreakAfterCloseTag(boolean).
Otherwise, you need to write a subclass like this:

private static class BreakingSerializer extends Serializer {
  public BreakingSerializer(OutputStream out, String encoding)
	throws UnsupportedEncodingException {
	super(out, encoding);

    protected void writeCloseTag(Element element)
            throws IOException {

    protected void writeEmptyElementTag(Element element)
            throws IOException {

I know that you're opposed to spurious convenience methods, but I think that
the effort required to implement the solution is sufficiently high (19
lines), that it would be worthwhile.


Dirk Bergstrom               dirk at juniper.net
Juniper Networks Inc.,          Computer Geek
Tel: 408.745.3182           Fax: 408.745.8905

More information about the XOM-interest mailing list