• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

how to combine xml and xsl files

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have an XSL file that can transform an XML file to HTML.
i want to use both these files in a servlet that can combine them and output the resultant html.
can you please help me with the same.
thanks in advance.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
import javax.xml.transform.sax.SAXSource;
import org.xml.sax.helpers.XMLReaderFactory;
import org.xml.sax.XMLReader;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.Result;


import java.io.*;
import java.net.*;
import java.util.*;

import javax.servlet.*;
import javax.servlet.http.*;

public class TransformServlet extends HttpServlet {

private InputStream sXmlFilePath;
private InputStream sXSLFilePath;
private static final String parserClass = "org.apache.xerces.parsers.SAXParser";


protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

ServletContext sc = getServletContext();
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();



sXmlFilePath = sc.getResourceAsStream("/xml/yourxml.xml");
sXSLFilePath = sc.getResourceAsStream("/xsl/yourxsl.xsl");

try{
XMLReader reader = XMLReaderFactory.createXMLReader(parserClass);
Source source = new SAXSource(reader, new InputSource(sXmlFilePath));
Source style = new StreamSource(sXSLFilePath);
TransformerFactory transFactory = TransformerFactory.newInstance();
Transformer trans = transFactory.newTransformer(style);
StreamResult result = new StreamResult(new StringWriter());
trans.transform(source, result);
String htmlString = result.getWriter().toString();
out.write(htmlString);
}catch(SAXException se){

System.out.println("SAXException"+se);

}catch(TransformerConfigurationException tce){

System.out.println("TransformerConfigurationException"+tce);

}catch(TransformerException te){
System.out.println("TransformerException"+te);

}

}


protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

public String getServletInfo() {
return "Short description";
}
// </editor-fold>
}
 
Sheriff
Posts: 28401
100
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would take this:and replace it by this:and remove the code that copied the string, thus writing the HTML directly to the servlet's output. That avoids having to materialize the entire output in memory, and it also avoids problems that arise when the output's assumed encoding doesn't match the system's default encoding.
 
Purnil Soni
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks a lot.
that was really quick and helpful.
three cheers to all.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic