Skip to content

Instantly share code, notes, and snippets.

@dennysfredericci
Created April 10, 2014 20:56
Show Gist options
  • Save dennysfredericci/10422562 to your computer and use it in GitHub Desktop.
Save dennysfredericci/10422562 to your computer and use it in GitHub Desktop.
A simple XML Formatter
import java.io.File;
import java.io.FileWriter;
import org.jdom2.Document;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;
public class XmlFormatter
{
public void format(File file) throws Exception
{
SAXBuilder sax = new SAXBuilder();
Document doc = sax.build(file);
FileWriter fileWriter = new FileWriter(file);
XMLOutputter xmlOutput = getOutputter();
xmlOutput.output(doc, fileWriter);
fileWriter.close();
}
private XMLOutputter getOutputter()
{
Format format = Format.getPrettyFormat();
format.setEncoding("UTF-8");
XMLOutputter xmlOutput = new XMLOutputter(format);
xmlOutput.setFormat(Format.getPrettyFormat());
return xmlOutput;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment