Created
November 6, 2018 10:37
-
-
Save jarryDk/b245bba7291002b988573e92249d14d2 to your computer and use it in GitHub Desktop.
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
package dk.jarry.util; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.FileWriter; | |
import java.io.InputStream; | |
import java.io.Writer; | |
import javax.xml.parsers.DocumentBuilder; | |
import javax.xml.parsers.DocumentBuilderFactory; | |
import javax.xml.transform.OutputKeys; | |
import javax.xml.transform.Transformer; | |
import javax.xml.transform.TransformerFactory; | |
import javax.xml.transform.dom.DOMSource; | |
import javax.xml.transform.stream.StreamResult; | |
import org.w3c.dom.Document; | |
public class XmlUtil { | |
public static void prettyPrint(File file) throws Exception { | |
if (file == null || !file.isFile()) { | |
throw new IllegalArgumentException("file skal være af typen File"); | |
} | |
InputStream in = null; | |
try { | |
in = new FileInputStream(file); | |
prettyPrint(in, file); | |
} finally { | |
if (in != null) { | |
try { | |
in.close(); | |
} catch (Exception e) { | |
// Do Nothing | |
} | |
} | |
} | |
} | |
public static void prettyPrint(File inFile, File outFile) throws Exception { | |
if (inFile == null || !inFile.isFile()) { | |
throw new IllegalArgumentException("inFile skal være af typen File"); | |
} | |
if (outFile == null || !outFile.isFile()) { | |
throw new IllegalArgumentException("outFile skal være af typen File"); | |
} | |
InputStream in = null; | |
try { | |
in = new FileInputStream(inFile); | |
prettyPrint(in, outFile); | |
} finally { | |
if (in != null) { | |
try { | |
in.close(); | |
} catch (Exception e) { | |
// Do Nothing | |
} | |
} | |
} | |
} | |
public static void prettyPrint(InputStream in, File outFile) throws Exception { | |
if (in == null) { | |
throw new IllegalArgumentException("in må ikke være null"); | |
} | |
if (outFile == null || !outFile.isFile()) { | |
throw new IllegalArgumentException("outFile skal være af typen File"); | |
} | |
Writer out = null; | |
try { | |
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); | |
dbf.setValidating(false); | |
DocumentBuilder db = dbf.newDocumentBuilder(); | |
Document document = db.parse(in); | |
Transformer tf = TransformerFactory.newInstance().newTransformer(); | |
tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); | |
tf.setOutputProperty(OutputKeys.INDENT, "yes"); | |
tf.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); | |
out = new FileWriter(outFile); | |
tf.transform(new DOMSource(document), new StreamResult(out)); | |
} catch (Exception e) { | |
throw e; | |
} finally { | |
if (out != null) { | |
try { | |
out.close(); | |
} catch (Exception e) { | |
// Do Nothing | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment