@Test public void testDocumentToXml() throws SAXException, IOException { final String xml1 = DOMUtils.documentToXml(DOCUMENT1); final String xml2 = DOMUtils.documentToXml(DOCUMENT2); assertNotNull(xml1); assertNotNull(xml2); assertTrue(StringUtils.hasText(xml1)); assertTrue(StringUtils.hasText(xml2)); assertXMLEqual(POLICY_STRING1, xml1); assertXMLEqual(POLICY_STRING2, xml2); }
public static String documentToXml(Document xmlDocument) { try { String xmlString = ""; final TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer; transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, DEFAULT_TRANSFORMER_PROPERTY_OMIT_XML_DECLARATION); transformer.setOutputProperty(OutputKeys.METHOD, DEFAULT_TRANSFORMER_PROPERTY_METHOD); transformer.setOutputProperty(OutputKeys.INDENT, DEFAULT_TRANSFORMER_PROPERTY_INDENT); transformer .setOutputProperty(OutputKeys.ENCODING, DEFAULT_ENCODING); transformer.setOutputProperty( "{http: DEFAULT_TRANSFORMER_PROPERTY_INDENT_AMOUNT); final StringWriter writer = new StringWriter(); transformer.transform(new DOMSource(xmlDocument), new StreamResult( writer)); xmlString = writer.getBuffer().toString().replaceAll("\n|\r", ""); Assert.hasText(xmlString, "Document object cannot be converted to XML string!"); return xmlString; } catch (TransformerException | IllegalArgumentException e) { throw new DOMUtilsException(e); } }
DOMUtils { public static String documentToXml(Document xmlDocument) { try { String xmlString = ""; final TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer; transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, DEFAULT_TRANSFORMER_PROPERTY_OMIT_XML_DECLARATION); transformer.setOutputProperty(OutputKeys.METHOD, DEFAULT_TRANSFORMER_PROPERTY_METHOD); transformer.setOutputProperty(OutputKeys.INDENT, DEFAULT_TRANSFORMER_PROPERTY_INDENT); transformer .setOutputProperty(OutputKeys.ENCODING, DEFAULT_ENCODING); transformer.setOutputProperty( "{http: DEFAULT_TRANSFORMER_PROPERTY_INDENT_AMOUNT); final StringWriter writer = new StringWriter(); transformer.transform(new DOMSource(xmlDocument), new StreamResult( writer)); xmlString = writer.getBuffer().toString().replaceAll("\n|\r", ""); Assert.hasText(xmlString, "Document object cannot be converted to XML string!"); return xmlString; } catch (TransformerException | IllegalArgumentException e) { throw new DOMUtilsException(e); } } }
DOMUtils { public static String documentToXml(Document xmlDocument) { try { String xmlString = ""; final TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer; transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, DEFAULT_TRANSFORMER_PROPERTY_OMIT_XML_DECLARATION); transformer.setOutputProperty(OutputKeys.METHOD, DEFAULT_TRANSFORMER_PROPERTY_METHOD); transformer.setOutputProperty(OutputKeys.INDENT, DEFAULT_TRANSFORMER_PROPERTY_INDENT); transformer .setOutputProperty(OutputKeys.ENCODING, DEFAULT_ENCODING); transformer.setOutputProperty( "{http: DEFAULT_TRANSFORMER_PROPERTY_INDENT_AMOUNT); final StringWriter writer = new StringWriter(); transformer.transform(new DOMSource(xmlDocument), new StreamResult( writer)); xmlString = writer.getBuffer().toString().replaceAll("\n|\r", ""); Assert.hasText(xmlString, "Document object cannot be converted to XML string!"); return xmlString; } catch (TransformerException | IllegalArgumentException e) { throw new DOMUtilsException(e); } }  }
DOMUtils { public static String documentToXml(Document xmlDocument) { try { String xmlString = ""; final TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer; transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, DEFAULT_TRANSFORMER_PROPERTY_OMIT_XML_DECLARATION); transformer.setOutputProperty(OutputKeys.METHOD, DEFAULT_TRANSFORMER_PROPERTY_METHOD); transformer.setOutputProperty(OutputKeys.INDENT, DEFAULT_TRANSFORMER_PROPERTY_INDENT); transformer .setOutputProperty(OutputKeys.ENCODING, DEFAULT_ENCODING); transformer.setOutputProperty( "{http: DEFAULT_TRANSFORMER_PROPERTY_INDENT_AMOUNT); final StringWriter writer = new StringWriter(); transformer.transform(new DOMSource(xmlDocument), new StreamResult( writer)); xmlString = writer.getBuffer().toString().replaceAll("\n|\r", ""); Assert.hasText(xmlString, "Document object cannot be converted to XML string!"); return xmlString; } catch (TransformerException | IllegalArgumentException e) { throw new DOMUtilsException(e); } }  static Document bytesToDocument(byte[] bytes); static byte[] documentToBytes(Document xmlDocument); static String documentToXml(Document xmlDocument); static Optional<Node> getNode(Document xmlDocument,
			String xPathExpr, String... arguments); static Optional<NodeList> getNodeList(Document xmlDocument,
			String xPathExpr, String... arguments); static Document xmlToDocument(String xmlString); }
DOMUtils { public static String documentToXml(Document xmlDocument) { try { String xmlString = ""; final TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer; transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, DEFAULT_TRANSFORMER_PROPERTY_OMIT_XML_DECLARATION); transformer.setOutputProperty(OutputKeys.METHOD, DEFAULT_TRANSFORMER_PROPERTY_METHOD); transformer.setOutputProperty(OutputKeys.INDENT, DEFAULT_TRANSFORMER_PROPERTY_INDENT); transformer .setOutputProperty(OutputKeys.ENCODING, DEFAULT_ENCODING); transformer.setOutputProperty( "{http: DEFAULT_TRANSFORMER_PROPERTY_INDENT_AMOUNT); final StringWriter writer = new StringWriter(); transformer.transform(new DOMSource(xmlDocument), new StreamResult( writer)); xmlString = writer.getBuffer().toString().replaceAll("\n|\r", ""); Assert.hasText(xmlString, "Document object cannot be converted to XML string!"); return xmlString; } catch (TransformerException | IllegalArgumentException e) { throw new DOMUtilsException(e); } }  static Document bytesToDocument(byte[] bytes); static byte[] documentToBytes(Document xmlDocument); static String documentToXml(Document xmlDocument); static Optional<Node> getNode(Document xmlDocument,
			String xPathExpr, String... arguments); static Optional<NodeList> getNodeList(Document xmlDocument,
			String xPathExpr, String... arguments); static Document xmlToDocument(String xmlString); static final String DEFAULT_ENCODING; }