From f824da90ba31a87f1331a3f4c36ae0f4fffc846e Mon Sep 17 00:00:00 2001 From: Priyesh Mashelkar Date: Fri, 12 Oct 2018 05:59:44 +0100 Subject: [PATCH] Converting DOM Document to File Issue: BAEL-2167 --- .../com/baeldung/xml/XMLDocumentWriter.java | 41 +++++++++++++++ .../xml/XMLDocumentWriterUnitTest.java | 52 +++++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 xml/src/main/java/com/baeldung/xml/XMLDocumentWriter.java create mode 100644 xml/src/test/java/com/baeldung/xml/XMLDocumentWriterUnitTest.java diff --git a/xml/src/main/java/com/baeldung/xml/XMLDocumentWriter.java b/xml/src/main/java/com/baeldung/xml/XMLDocumentWriter.java new file mode 100644 index 0000000000..e33fc5fe1d --- /dev/null +++ b/xml/src/main/java/com/baeldung/xml/XMLDocumentWriter.java @@ -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); + } + } +} diff --git a/xml/src/test/java/com/baeldung/xml/XMLDocumentWriterUnitTest.java b/xml/src/test/java/com/baeldung/xml/XMLDocumentWriterUnitTest.java new file mode 100644 index 0000000000..68f787a054 --- /dev/null +++ b/xml/src/test/java/com/baeldung/xml/XMLDocumentWriterUnitTest.java @@ -0,0 +1,52 @@ +package com.baeldung.xml; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; + +import org.apache.commons.io.FileUtils; +import org.junit.After; +import org.junit.Test; +import org.w3c.dom.Document; +import org.w3c.dom.Element; + +import java.io.File; + +public class XMLDocumentWriterUnitTest { + + @Test + public void givenXMLDocumentWhenWriteIsCalledThenXMLIsWrittenToFile() throws Exception { + Document document = createSampleDocument(); + new XMLDocumentWriter().write(document, "company_simple.xml", false, false); + } + + @Test + public void givenXMLDocumentWhenWriteIsCalledWithPrettyPrintThenFormattedXMLIsWrittenToFile() throws Exception { + Document document = createSampleDocument(); + new XMLDocumentWriter().write(document, "company_prettyprinted.xml", false, true); + } + + private Document createSampleDocument() throws ParserConfigurationException { + DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); + DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); + Document document = documentBuilder.newDocument(); + Element companyElement = document.createElement("Company"); + document.appendChild(companyElement); + Element departmentElement = document.createElement("Department"); + departmentElement.setAttribute("name", "Sales"); + companyElement.appendChild(departmentElement); + Element employee1 = document.createElement("Employee"); + employee1.setAttribute("name", "John Smith"); + departmentElement.appendChild(employee1); + Element employee2 = document.createElement("Employee"); + employee2.setAttribute("name", "Tim Dellor"); + departmentElement.appendChild(employee2); + return document; + } + + @After + public void cleanUp() throws Exception { + FileUtils.deleteQuietly(new File("company_simple.xml")); + FileUtils.deleteQuietly(new File("company_prettyprinted.xml")); + } +}