Tuesday, April 10, 2012

Java: Using IndentingXMLStreamwriter results in large file size

I am using the IndentingXMLStreamWriter-Class to get a well formatted XML-file. The code looks like this:



XMLOutputFactory output = XMLOutputFactory.newInstance();
XMLStreamWriter streamWriter;
IndentingXMLStreamWriter indentingStreamWriter;
streamWriter = output.createXMLStreamWriter(new FileOutputStream(fileName));
indentingStreamWriter = new IndentingXMLStreamWriter(streamWriter);
indentingStreamWriter.setIndentStep(" ");
indentingStreamWriter.writeStartDocument("UTF-8", "1.0");


The size and depth of the generated XML is very large. When I use the indenting streamwriter the size of the file is more than twice as large as the file generated with the "normal" streamwriter. Is there are better way to realize the indenting or am i doing something wrong?



Thanks in advance



Lars





1 comment:

  1. At the risk of sounding stupid, you're taking a file of XML and adding indenting (4 characters per level) and newlines (presumably). It's going to get bigger.

    How much bigger depends on how big the XML tags are and how deeply nested they are. If they tags are quite small, then the relative size of the whitespace is quite large and could conceivably double the size of the file. If they're deeply nested, you'll be adding lots of space to each line.

    You don't appear to have many options if you want a pretty-printed document. You could use fewer spaces (2 instead of 4), or even tab characters (although the next guy might shoot you for doing so), but, really, I think this is a case of c'est la vie.

    ReplyDelete