In addition to simply outputting XML as a stand-alone library with the SimpleXmlWriter, XmlWriter has other functionalities available.
Instead of SimpleXmlWriter, the user may choose to use Ernst de Haan's xmlenc project as the underlying writer. Apart from probable better performance, xmlenc also allows you to specify the text encoding, which SimpleXmlWriter does not.
XmlWriter out = new XmlEncXmlWriter(writer)
XmlWriter out = new XmlEncXmlWriter(writer, encoding)
For this functionality, the xmlenc jar needs to be in the classpath.
The rest of these functionalities are all wrappers [or DelegatingXmlWriters] rather than replacements for SimpleXmlWriter. The first wrapper functionality is pretty printing.
PrettyPrinterXmlWriter pretty = new PrettyPrinterXmlWriter( xmlwriter );
pretty.setIndent("\t");
pretty.setNewline("\n\n");
The default indent is two spaces, and newline is a simple '\n' newline.
Printing numbers and dates can be a pain, so the FormattingXmlWriter helps make it easy.
FormattingXmlWriter form = new FormattingXmlWriter( xmlwriter );
form.setNumberFormat( NumberFormat.getCurrencyInstance() );
form.setDateFormat( DateFormat.getDateInstance() );
Currently the Formatting writer does not know the difference between Numbers and will use the same NumberFormat for Floats, Doubles, Longs etc.
There are various different strategies to handling null and empty-string data. The EmptyElementXmlWriter allows a strategy to be chosen.
EmptyElementXmlWriter eexw = new EmptyElementXmlWriter( xmlwriter );
eexw.setEmptyMode(EmptyElementXmlWriter.NULL_EMPTY_MODE);
There are three modes to choose from. EMPTY_MODE considers null and "" to be empty and will not output attributes or tags with names or values that are empty. NULL_EMPTY_MODE considers only null values to be empty, and will print out "", while IGNORE_EMPTY_MODE is the equivalent of not using the EmptyElementXmlWriter. EMPTY_MODE is the default.
DTD and XML Schema are often checked when parsing XML, but rarely checked when outputting XML, even when using the DOM style. The JarvWriter takes a JARV VerifierHandler and checks the XmlWriter output on the fly, currently printing to System.err when a problem happens.
To use on the fly schema checking, the isorelax and xerces jars need to be on the classpath. Currently JarvWriter is a proof of concept, thus the System.err printing.