DocumentXmlConverterImpl implements DocumentXmlConverter { @Override public String convertXmlDocToString(Document xmlDocument) throws DocumentXmlConverterException { String xmlString = ""; TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer; try { transformer = tf.newTransformer(); transformer .setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.setOutputProperty( "{http: StringWriter writer = new StringWriter(); transformer.transform(new DOMSource(xmlDocument), new StreamResult( writer)); xmlString = writer.getBuffer().toString().replaceAll("\n|\r", ""); } catch (TransformerException e) { throw new DocumentXmlConverterException(e); } return xmlString; }  @Override Document loadDocument(String xmlString); @Override String convertXmlDocToString(Document xmlDocument);  }
@Test public void testConverXmlDocToString_1() throws Exception { Document document = readDocument("src/test/resources/xmlDocument.txt"); String docString = documentXmlConverter.convertXmlDocToString(document); logger.debug(docString); assertNotNull(docString); String s1 = fileReader.readFile("xmlString1.txt"); assertNotNull(s1, "s1 is null"); assertNotNull(docString, "docString is null"); assertTrue(XmlComparator.compareXMLs(s1, docString, Arrays.asList("")) .similar()); }
@Test public void testConverXmlDocToString_2() throws Exception { Document document = readDocument("src/test/resources/xmlDocument.txt"); String docString = documentXmlConverter.convertXmlDocToString(document); logger.debug(docString); assertNotNull(docString); String s2 = fileReader.readFile("xmlString2.txt"); assertTrue(XmlComparator.compareXMLs(s2, docString, Arrays.asList("")) .similar()); }
@Test public void testConverXmlDocToString_3() throws Exception { Document document = readDocument("src/test/resources/xmlDocument.txt"); String docString = documentXmlConverter.convertXmlDocToString(document); logger.debug(docString); assertNotNull(docString); String s3 = fileReader.readFile("xmlString3.txt"); assertTrue(XmlComparator.compareXMLs(s3, docString, Arrays.asList("")) .similar()); }