[jdom-interest] newbie jdom -> xalan problem

Alan Eustace alan.eustace at bigfoot.com
Thu Apr 5 07:49:05 PDT 2001


I'd be very grateful if someone can shed any light on this;
I'm processing xml in a servlet, using piped streams as per the jdom faq
of about a month ago.
The parser hangs at the  "xmlOutputter.output( doc, pout );" line. I had
a similar problem before, which was to do
with not closing the piped output stream: adding the close() call solved
that, but I'm baffled by this problem. No exception is being thrown.

Many thanks,
Alan Eustace.

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.*;
import org.jdom.output.*;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;

// Xalan-J 2
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Templates;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.stream.StreamResult;



public class JDOMXalan extends HttpServlet{


  PrintWriter out;
  Element msg;
  Document xmldoc;
  Transformer processor;


  public void init(ServletConfig config)throws ServletException{
              super.init(config);

            try{

              File xslt = new File("foo.xsl");
              SAXBuilder builder = new SAXBuilder();
              xmldoc = builder.build(new File("foo.xml"));
              Element root = xmldoc.getRootElement();
              msg = root.getChild("message");
              TransformerFactory transformerFactory =
TransformerFactory.newInstance();
              Templates stylesheet = transformerFactory.newTemplates(new
StreamSource( xslt ) );
              processor = stylesheet.newTransformer();

            }
            catch (Exception j){
                  j.printStackTrace();
            }
  }


  public void doGet(HttpServletRequest req, HttpServletResponse
res)throws
  ServletException, IOException{
        out = res.getWriter();
        res.setContentType("text/html");

      try{

      PipedInputStream in = new PipedInputStream();
      PipedOutputStream pout  = new PipedOutputStream( in );
      StreamSource source = new StreamSource( in );
      StreamResult outsource = new StreamResult (out);

      XMLOutputter xmlOutputter = new XMLOutputter();
       //***parser hangs here***
      xmlOutputter.output( xmldoc, pout  );

      pout.close();
      processor.transform( source, outsource );
      }
      catch (Exception e) {
         e.printStackTrace();
      }
  }
}




More information about the jdom-interest mailing list