Hi Arsalan

If you have an XML representation of HTML (e.g. XHTML) you might find the
HTMLWriter useful - it expands CDATA sections to text and avoids closing
some tags which are known to cause layout problems (e.g. <p>ffff </p> or
<br></br>, <img>foo </img> and so on)

    response.setContentType("text/html");
    PrintWriter out = response.getWriter();

    HTMLWriter writer = new HTMLWriter( out );
    writer.write( transformedDoc );

(you could use XMLWriter rather than the HTMLWriter instead to preserve the
XML-ness of your document).


Alternatively, if you have a dom4j Document that you wish to style into some
visual representation using XSLT and immediately return the output of the
transformation to the user, you could bypass creating a dom4j Document with
the result and just use JAXP to write the textual output. e.g.

        Document document = ...;

        // load the transformer
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer(
            new StreamSource( xsl )
        );

        // the source is a dom4j document
        Source source = new DocumentSource( document );

        // the result is the Servlet response
        Result result = new StreamResult( response.getWriter() );

        // now lets transform
        transformer.transform( source, result );


Also the reverse can be done - if there's an external XML document that you
wish to read from a stream, transform it with XSLT and then process the
result programatically with dom4j you could do this in reverse. The
following example takes the servlet input (e.g. a HTTP POST) then transforms
it and creates a dom4j Document with the result...

        // the source is the servlet input (POST)
        Source source = new StreamSource( request.getReader() );

        // the result will be a dom4j Document
        DocumentResult result = new DocumentResult();

        // now lets transform
        transformer.transform( source, result );

        Document transformedDoc = result.getDocument();


James

----- Original Message -----
From: Thomas Nichols
To: Lodhi, arsalan ; [EMAIL PROTECTED]
Sent: Monday, June 18, 2001 4:50 PM
Subject: Re: [dom4j-user] Sending the Transformed Document Back


Arsalan,
Try either
out.println (transformedDoc.asXML());
or use the XMLWriter and OutputFormat classes for better control.
Is this XHTML you are returning? If raw XML, make sure your browser can
display it ok - try writing transformedDoc to a file and open it from there.

Regards,
Thomas.


At 15:26 18/06/2001 +0000, Lodhi, arsalan wrote:

Does anyone know how can we send the document back to browser if I am using
servlet - I've done this:

  response.setContentType("text/html");
  PrintWriter out = response.getWriter();
  out.println(transformedDoc);
  out.close();

but out.println(String ) takes string - how can i send the transformed (from
xml - html) document back to browser

thanks
arsalan




Get your FREE download of MSN Explorer at http://explorer.msn.com
_______________________________________________ dom4j-user mailing list
[EMAIL PROTECTED]
http://lists.sourceforge.net/lists/listinfo/dom4j-user
_______________________________________________ dom4j-user mailing list
[EMAIL PROTECTED]
http://lists.sourceforge.net/lists/listinfo/dom4j-user


_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com


_______________________________________________
dom4j-user mailing list
[EMAIL PROTECTED]
http://lists.sourceforge.net/lists/listinfo/dom4j-user

Reply via email to