Nested XML Transformers

I need to use the result of javax.xml.transform.Transformer as an input to another transformer without saving the results to a file. It...

Reader input = new StringReader(xml); // Where xml is a String
StringWriter output = new StringWriter();
StreamSource source = new StreamSource(input);
StreamResult result = new StreamResult(output);

transformer1.transform(source1, result1);

// Get contents of result1 into source2

transformer2.transform(source2, result2);
+3
source share
2 answers

Replace

// Get contents of result1 into source2

with

input2 = new StringReader(output1.getBuffer().toString());
source2 = new StreamSource(input2);
output2 = new StringWriter();
result2 = new StreamResult(output2);
+1
source

You can make result1 DOMResult, and then get the DOM from it after the first conversion and use it to make source2 DOMSource for the second conversion.

0
source

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


All Articles