Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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