You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xalan.apache.org by Bennett <be...@glengroup.com> on 2003/07/09 22:04:36 UTC

How to return transform output

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



Re: How to return transform output

Posted by Joseph Kesselman <ke...@us.ibm.com>.



> I've investigated the org.apache.xml.serializer package but haven't
> found anything that returns a string.

You were almost there: Construct a StringWriter and wrap the StreamResult
around that rather than around a FileOutputStream, run the transform, then
retrieve the text from the StringWriter.



______________________________________
Joe Kesselman, IBM Next-Generation Web Technologies: XML, XSL and more.
"The world changed profoundly and unpredictably the day Tim Berners Lee
got bitten by a radioactive spider." -- Rafe Culpin, in r.m.filk