如何使用JavaAPI使用XSLT解析xml字符串并仅在内存中生成输出?

如何使用JavaAPI使用XSLT解析xml字符串并仅在内存中生成输出?,java,xml,parsing,xslt,Java,Xml,Parsing,Xslt,我需要使用预定义的XSLT解析内部XML(来自响应),并将解析结果以html形式发送回客户端。我注意到以下使用和生成本地文件的示例。如何避免使用JavaAPI创建文件?我想用字符串替换source.xml并动态生成html输出 TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer (new javax.xml.tra

我需要使用预定义的XSLT解析内部XML(来自响应),并将解析结果以html形式发送回客户端。我注意到以下使用和生成本地文件的示例。如何避免使用JavaAPI创建文件?我想用字符串替换source.xml并动态生成html输出

  TransformerFactory tFactory = TransformerFactory.newInstance();
  Transformer transformer = tFactory.newTransformer (new javax.xml.transform.stream.StreamSource("searchresult.xslt"));
   transformer.transform(new javax.xml.transform.stream.StreamSource("source.xml"),
           new javax.xml.transform.stream.StreamResult( new FileOutputStream("result.html")));
StreamSource有一个参数。因此,您可以传递一个StringReader作为参数,它将从字符串中读取XML


类似地,将OutputStream作为参数。因此,您可以传递任何类型的OutputStream(如HTTP响应输出流、ByteArrayOutputStream或套接字输出流)以将结果发送到您喜欢的任何地方。

要使用StreamResult,请使用StringWriter而不是FileOutputStream。谢谢!我会试试看。