Converting DOM Document to File
Issue: BAEL-2167
This commit is contained in:
committed by
Josh Cummings
parent
90867eb6b4
commit
f824da90ba
@@ -0,0 +1,41 @@
|
||||
package com.baeldung.xml;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.xml.transform.OutputKeys;
|
||||
import javax.xml.transform.Transformer;
|
||||
import javax.xml.transform.TransformerConfigurationException;
|
||||
import javax.xml.transform.TransformerException;
|
||||
import javax.xml.transform.TransformerFactory;
|
||||
import javax.xml.transform.dom.DOMSource;
|
||||
import javax.xml.transform.stream.StreamResult;
|
||||
|
||||
import org.w3c.dom.Document;
|
||||
|
||||
public class XMLDocumentWriter {
|
||||
|
||||
public void write(Document document, String fileName, boolean excludeDeclaration, boolean prettyPrint) {
|
||||
try(FileWriter writer = new FileWriter(new File(fileName))) {
|
||||
TransformerFactory transformerFactory = TransformerFactory.newInstance();
|
||||
Transformer transformer = transformerFactory.newTransformer();
|
||||
if(excludeDeclaration) {
|
||||
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
|
||||
}
|
||||
if(prettyPrint) {
|
||||
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
|
||||
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
|
||||
}
|
||||
DOMSource source = new DOMSource(document);
|
||||
StreamResult result = new StreamResult(writer);
|
||||
transformer.transform(source, result);
|
||||
} catch (IOException e) {
|
||||
throw new IllegalArgumentException(e);
|
||||
} catch (TransformerConfigurationException e) {
|
||||
throw new IllegalStateException(e);
|
||||
} catch (TransformerException e) {
|
||||
throw new IllegalArgumentException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user