Code example for StreamResult

0
 
    private String domToString(Document document) throws TransformerException {
        StringWriter writer = new StringWriter();
        Transformer transformer = TransformerFactory.newInstance() .newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.transform(new DOMSource(document), new StreamResult(writer));
        return writer.toString();
    } 
 
    private class ErrorRecorder implements DOMErrorHandler {
        private final List<DOMError> errors = new ArrayList<DOMError>();
 
        public boolean handleError(DOMError error) {
            errors.add(error);
            return true; 
        } 
 
        public void assertAllErrors(int severity, String type) {
            assertAllErrors("Expected one or more " + type + " errors", severity, type);
        }