I am trying to print out a transformed XML document using a Java Servlet (GlassFish Server). I am getting the xml from an external website, transforming it with XSLT, and then trying to print out the transformed document. I believe the the XSL file is correct and is performing a valid transformation.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.*;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.Result;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
/**
*
* @author Kirie
*/
public class getArticles extends HttpServlet {
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException, TransformerConfigurationException, TransformerException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
// get the xsl stored in this project
ServletContext context = getServletContext();
InputStream xsl = (InputStream)
(context.getResourceAsStream("/XSLTransformerCode.xsl"));
// We need two source objects and one result
// get an external xml document using a url in a
// string format
String topic = request.getParameter("topic");
String urlAsString = "";
if(topic.equals("Business"))
urlAsString = "http://ift.tt/1vDH79i";
else if(topic.equals("Technology"))
urlAsString = "http://ift.tt/1ap2UqG";
else if(topic.equals("World"))
urlAsString = "http://ift.tt/1o9GOtg";
Source xmlDoc = new StreamSource(urlAsString);
Source xslDoc = new StreamSource(xsl);
Result result = new StreamResult(out);
// Prepare to transform
// The transformed document is returned to the browser.
TransformerFactory factory = TransformerFactory.newInstance();
Transformer trans = factory.newTransformer(xslDoc);
trans.transform(xmlDoc,result);
out.println(result.toString());
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
processRequest(request, response);
} catch (TransformerException ex) {
Logger.getLogger(getArticles.class.getName()).log(Level.SEVERE, null, ex);
}
}
/**
* Handles the HTTP <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
processRequest(request, response);
} catch (TransformerException ex) {
Logger.getLogger(getArticles.class.getName()).log(Level.SEVERE, null, ex);
}
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
This is the servlet that is performing the translation. You can visit the XML documents from the URL specified above. They are RSS feeds.
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://ift.tt/tCZ8VR"
>
<xsl:output method="xml"
omit-xml-declaration="yes"
doctype-system="http://ift.tt/mOIMeg"
doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"/>
<xsl:template match="/">
<html><head></head>
<body>
<h1>News Articles</h1>
<ul>
<xsl:for-each select = "catalog/item">
<li>
<xsl:value-of select = "title"/>
</li>
</xsl:for-each>
</ul>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
This is the XSL file I am using to transform the file. Unfortunately, I do not know where the problem is. I have tried to figure things out, but I can't seem to do that. Any input will be helpful and appreciated. I'll be online for awhile so I will be able to answer questions!
Thanks in advance! Kirie
No comments:
Post a Comment