There is a very nice command line XML utility called xmlstarlet(http://xmlstar.sourceforge.net/) that can do a lot of things which a lot of people use.
You could execute this program programmatically using Runtime.exec and then read in the formatted output file. It has more options and better error reporting than a few lines of Java code can provide.
download xmlstarlet : http://sourceforge.net/project/showfiles.php?group_id=66612&package_id=64589