View Full Version : Jsp xml xsl
texerasmo
29-06-2004, 17:03
Qualcuno di voi ha mai fatto trasformazioni xml xsl utilittando jsp?
io le ho sempre fatte con toll oppure utilizzando cocoon.
texerasmo
29-06-2004, 17:20
Questo è un esempio ma manca ancora la gestione dei parametri per xsl
<%@ page import="javax.servlet.jsp.JspWriter, java.sql.*, java.util.*"%>
<%@ page import="javax.xml.transform.*"%>
<%@ page import="javax.xml.transform.stream.*"%>
<%@ page import="java.io.*"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Documento senza titolo</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<%
String path = "c:";
String xmlfile = path+"/prova.xml";
String xslfile = path+"/prova.xsl";
out.write("nome file xml "+xmlfile);
out.write("<br>");
out.write("nome file xsl "+xslfile);
out.write("<br>");
String testo = creaHtml(xmlfile,xslfile,path);
out.write("Risultato. -->");
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer(new StreamSource(xslfile));
transformer.transform(new StreamSource(xmlfile), new StreamResult(out));
%>
<%
try{
// apre il file copywrite.txt in scrittura
FileWriter fileout = new FileWriter(getServletContext().getRealPath("/")+"prova.html");
fileout.write(testo);
fileout.close(); // chiude il file
}catch (Exception e){
out.write("errore ");
}
%>
</body>
</html>
<%!
public String creaHtml(String fileXml,String xslFile,String percorsoXml)
{
StringWriter fileHtml = new StringWriter();
try
{
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer(new StreamSource(xslFile));
transformer.setParameter("param1",percorsoXml);
transformer.transform(new StreamSource(fileXml), new StreamResult(fileHtml));
}
catch(Exception exc)
{
return "Errore:"+ exc.getMessage();
}
return fileHtml.toString();
}
%>
vBulletin® v3.6.4, Copyright ©2000-2026, Jelsoft Enterprises Ltd.