You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cocoon.apache.org by Donald Ball <ba...@webslingerZ.com> on 2000/05/26 07:07:44 UTC

Re: Using the SQL Processor from a servlet

On Fri, 26 May 2000, Russell Castagnaro wrote:

> Does anyone know how I can make an XML document in a servlet, that use
> the SQL processor on it to get a new XML doc? Thanks in advanced!

You could write a custom producer that called your servlet's service
method, and have the XML document you create call the SQL processor
through the <?cocoon-process?> PI. Or you could call your servlet from an
XSP page and use the SQL taglib instead.

- donald


Re: Using the SQL Processor from a servlet

Posted by Russell Castagnaro <ru...@synctank.com>.
Donald,
Thanks for the advice!  I must've been suffering from a short lapse, but you
note triggered a mental reboot!

Here's the service method code for the rest of you that might want to know how
to do this:
Note this also transforms the resulting doc via xslt.


 public void service(HttpServletRequest _req, HttpServletResponse _res)
        throws IOException, ServletException {

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

        // find out what doc and stylesheet we're dealing with here

        String xml = _req.getParameter("XML");
        String xsl = _req.getParameter("XSL");
        if (xml == null) xml = defaultXML;
        if (xsl == null) xsl = defaultXSL;


        // Get the XML Document
        File xmlFile = new File(sourcePath,xml);
        File xslFile = new File(sourcePath,xsl);

        try {

           XercesParser parser = new XercesParser();
     Document sqldoc = parser.parse(new XSLTInputSource(new
FileReader(xmlFile)));
     log("XML "+xml +" parsed.");
     Hashtable parms = new Hashtable();
     parms.put("request",_req);
     parms.put("response",_res);
     SQLProcessor sqlProcessor = new SQLProcessor();
     Document doc = sqlProcessor.process(sqldoc,parms);
     XSLTProcessor processor = XSLTProcessorFactory.getProcessor();

     processor.process(new XSLTInputSource(doc),
                        new XSLTInputSource(new FileReader(xslFile)),
                        new XSLTResultTarget(out));
        } catch (Exception e) {
            e.printStackTrace(out);
        }

        out.flush();

    }


Donald Ball wrote:

> On Fri, 26 May 2000, Russell Castagnaro wrote:
>
> > Does anyone know how I can make an XML document in a servlet, that use
> > the SQL processor on it to get a new XML doc? Thanks in advanced!
>
> You could write a custom producer that called your servlet's service
> method, and have the XML document you create call the SQL processor
> through the <?cocoon-process?> PI. Or you could call your servlet from an
> XSP page and use the SQL taglib instead.
>
> - donald

--
Hope to see you at my presentation, "Making the Most of XML, XSL and Server-Side
Processing"
at XML DevCon 2000, June 25-28, 2000.

Russell Castagnaro
Chief Mentor
SyncTank Solutions
http://www.synctank.com

Earth is the cradle of mankind; one does not remain in the cradle forever
-Tsiolkovsky