Easy way to convert DOMSource to StreamSource?

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

In this case, performance is not so much a problem, so I came up with this terribly detailed 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); 

Is there an easier way to do this?

+6
source share
1 answer

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

+6
source

Source: https://habr.com/ru/post/912210/


All Articles