try {
if (result instanceof DOMResult) {
Node node = ((DOMResult) result).getNode();
if (node instanceof Document) {
writer = new W3CDOMStreamWriter((Document) node);
} else if (node instanceof Element) {
writer = new W3CDOMStreamWriter((Element) node);
} else {
throw new UnsupportedOperationException("Node type not supported.");
}
} else {
writer = xmlOutputFactory.createXMLStreamWriter(result);