prettyprint XML provides a java utility to do just that with support for attribute indentation and sorting, JDOM output format options and stylesheet transformations.
Project objectives are to provide a library that accepts a input stream of characters and prints them with appropriate indentations and line breaks. The library should be balance between ease of use, flexibility of format, and optimality of output.