Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simpler way to transform a DOMSource into a StreamSource?

I need to transform a DOMSource into a StreamSource, because a third-party library only accepts stream sources for SOAP.

Performance is not so much of an issue in this case, so I came up with this horribly verbose set of commands:

DOMSource src = new DOMSource(document);
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
StreamResult result = new StreamResult();
ByteArrayOutputStream out = new ByteArrayOutputStream();
result.setOutputStream(out);
transformer.transform(src, result);
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
StreamSource streamSource = new StreamSource(in);

Isn't there a simpler way to do this?

like image 570
Lukas Eder Avatar asked Apr 02 '12 08:04

Lukas Eder


1 Answers

This is as good a way as any. Because your third party library only accepts XML in lexical form, you have no alternative but to serialize the DOM so that the external library can re-parse it. Stupid design - tell them so.

like image 197
Michael Kay Avatar answered Oct 23 '22 02:10

Michael Kay