Created
April 10, 2014 20:56
-
-
Save dennysfredericci/10422562 to your computer and use it in GitHub Desktop.
A simple XML Formatter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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