Hello,
I'm running into a situation where I need to get the output of a transform and return it from a Java function.
The importance of the return, as opposed to simply doing:
...
transformer.transform( new StreamSource(sourceXML), new StreamResult(new FileOutputStream(outputXML)));
...
is that I'm working on getting the transform results out of Java into a different programming environment where the return from the Java has to be a string.
I've investigated the org.apache.xml.serializer package but haven't found anything that returns a string.
Here is an example of what I'm trying to do:
public static String getTransform(String sourceXML, String sourceXSL)
throws TransformerException, FileNotFoundException
{
StringWriter tOutput = new StringWriter();
try
{
TransformerFactory tFactory = TransformerFactory.newInstance();
Templates translet = tFactory.newTemplates(new StreamSource(sourceXSL));
Transformer transformer = translet.newTransformer();
transformer.transform( new StreamSource(sourceXML), new StreamResult(tOutput));
}
catch (Exception e)
{
e.printStackTrace();
}
return tOutput.toString();
}
Of course, in this example, there is no return string from tOutput.toString() but, this the closest attempt so far in trying to return the transform output.
Does anybody have any code samples or resources that demonstrate how this could be done?
TIA, Bennett