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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With