On Tue, 2004-06-08 at 05:41, Dionisio Ruiz de Zarate wrote:
> Hola tengo el metodo de abajo que me imprime html pasandole un xml y una
> xslt desde una jsp.
>
> la transformacion, impresion en pagina se hace con:
> <jsp:useBean id="transformando" scope="session"
> class="com.utils.Transformacion" />
> <%
> //salida xml es un string xml y xslStyleSheet1 es la ruta a la xslt
> transformando.Transforma(salidaXML, xslStyleSheet1, response, out);
> %>
>
> bien esto funciona muy bien con el codigo de abajo.
> Pero quisiera que en vez de imprimir el html resultante me lo metiera en
> un String.
> Me podeis ayudar ha hacerlo?
> oseaa que el resultado de el codigo de abajo fuera un string con el codigo
> html resultate..
>
> Muchas gracias por leerme y por vuestra ayuda.
Crea un StringWriter:
StringWriter writer = new StringWriter(); // si sabes aprox. el tamaño del
resultado, úsalo en el constructor
StreamResult result = new StreamResult(writer);
Luego puedes obtener el String con "writer.toString()" o puedes obtener
el StringBuffer con "writer.getBuffer()"
> public void Transforma(String xml,
> String stylesheet,
> HttpServletResponse response,
> Writer out)
> throws TransformerException, TransformerConfigurationException,
> FileNotFoundException, IOException{
> DocumentBuilderFactory factory =
> DocumentBuilderFactory.newInstance();
> try {
> DocumentBuilder builder = factory.newDocumentBuilder();
> TransformerFactory tFactory = TransformerFactory.newInstance();
> byte b[] = (new String(xml)).getBytes();
> ByteArrayInputStream bitsxml = new ByteArrayInputStream(b);
> StreamSource stylesource = new StreamSource(stylesheet);
> Transformer transformer = tFactory.newTransformer(stylesource);
> StreamResult result = new StreamResult(out);
> transformer.transform(new StreamSource(bitsxml), result);
> } catch (TransformerConfigurationException tce) {
> // Error generated by the parser
> System.out.println("\n** Transformer Factory error");
> System.out.println(" " + tce.getMessage() );
>
> // Use the contained exception, if any
> Throwable x = tce;
> if (tce.getException() != null)
> x = tce.getException();
> x.printStackTrace();
> } catch (TransformerException te) {
> // Error generated by the parser
> System.out.println("\n** Transformation error");
> System.out.println(" " + te.getMessage() );
> // Use the contained exception, if any
> Throwable x = te;
> if (te.getException() != null)
> x = te.getException();
> x.printStackTrace();
> } catch (ParserConfigurationException pce) {
> // Parser with specified options can't be built
> pce.printStackTrace();
> }
> }
________________________________________
Robert Marcano
web: http://www.marcanoonline.com/
blog: http://www.marcanoonline.com/plog/
________________________________________