26/10/2012, 14:46
|
| | Fecha de Ingreso: noviembre-2007
Mensajes: 37
Antigüedad: 17 años, 2 meses Puntos: 0 | |
Servlet Hola, como puedo guardar lo que me muestara el servlet a un arvhico .xml?
Código:
package com.facturas.servlets;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.xml.serialize.OutputFormat;
import org.apache.xml.serialize.XMLSerializer;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.w3c.dom.Element;
import com.facturas.converters.ConverterEFACE;
/**
* Servlet implementation class FacturasG4S
*/
public class FacturasG4S extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* Default constructor.
*/
public FacturasG4S() {
// TODO Auto-generated constructor stub
}
private WebApplicationContext getContext()
{
ServletContext servletContext = this.getServletContext();
WebApplicationContext wac = WebApplicationContextUtils.
getRequiredWebApplicationContext(servletContext);
return wac;
}
private String printDOM(Element xmlDoc) throws Exception
{
String retorno = "";
OutputFormat outputFormat = new OutputFormat("XML","UTF-8",true);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
XMLSerializer xmlSerializer = new XMLSerializer(baos, outputFormat);
xmlSerializer.asDOMSerializer();
xmlSerializer.serialize(xmlDoc);
if(baos != null)
{
retorno = baos.toString();
}
return retorno;
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
try
{
ConverterEFACE converter = (ConverterEFACE)getContext().getBean("converterEFACE");
String facturaId = request.getParameter("id");
if((facturaId != null) && (!facturaId.equals("")))
{
Element elemento = converter.generateElement(Long.parseLong(facturaId));
response.getOutputStream().println(printDOM(elemento));
response.getOutputStream().close();
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
Última edición por palmach; 26/10/2012 a las 15:59 |