build.please.common.report.PrettyPrintingXmlWriter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of junit-runner Show documentation
Show all versions of junit-runner Show documentation
JUnit test runner for Please to run Java tests with.
The newest version!
package build.please.common.report;
import org.w3c.dom.Document;
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 java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
public class PrettyPrintingXmlWriter {
public static void writeXMLDocumentToFile(String filename, Document doc) throws Exception {
// Create an XML transformer that pretty-prints with a 2-space indent.
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer trans = transformerFactory.newTransformer();
trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
trans.setOutputProperty(OutputKeys.INDENT, "yes");
trans.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
File outputFile = new File(filename);
OutputStream output = new BufferedOutputStream(new FileOutputStream(outputFile));
StreamResult streamResult = new StreamResult(output);
DOMSource source = new DOMSource(doc);
trans.transform(source, streamResult);
output.close();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy