我总共有3个文件如下
见图片http://i.imgur.com/BUNtY3D.png
我的第一个任务是将 data.xml 和 render.xslt 作为输入,在此之后将输出为 All.xml 给定的servlet将处理 All.xml 和 last.xslt 作为输入,这将为Http Request提供输出响应。
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URL;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
/**
*
* @author Shubham Dalal
*/
public class Manager extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException,
java.net.MalformedURLException {
// Tell the response object that our servlet returns HTML
response.setContentType("text/html; charset=UTF-8");
// Ask for a response stream
PrintWriter output = response.getWriter();
// This block tries to open our XML and XSLT files
// which may throw an exception, thus the try..catch
try {
// Get a empty transform factory (factory pattern)
TransformerFactory tFactory = TransformerFactory.newInstance();
// Get the actual server path for XML and XSLT files
String loc = getServletContext().getRealPath("") + "/";
Source sdk2 = new StreamSource(
new URL("file", "", loc + "full.xml").openStream());
Source Main = new StreamSource(
new URL("file", "", loc + "Main.xslt").openStream());
// Generate the transformer
Transformer Maintransformer = tFactory.newTransformer(Main);
// GRAND FINALE!
// Perform the transformation, send output to the browser
Maintransformer.transform(sdk2, new StreamResult(output));
// Get the XML input document and the stylesheet using
// the string loc that holds the path information,
// creating a JAXP Source instance
Source sdk = new StreamSource(
new URL("file", "", loc + "sdk.xml").openStream());
Source list = new StreamSource(
new URL("file", "", loc + "list.xsl").openStream());
// Generate the transformer
Transformer listtransformer = tFactory.newTransformer(list);
listtransformer.transform(sdk, new StreamResult(new File("", loc + "full.xml")));
} catch (Exception e) {
output.write(e.getMessage());
e.printStackTrace(output);
}
output.close();
}
}
我是servlet的新手。