Dynamically generate XML using servlet
To dynamically generate XML using servlet below are needed:
- Eclipse 3.2 or above (Download eclipse from here) — We are using Eclipse Kepler for this demo
- JDK 1.6 or above (Download from here)
- Tomcat 6 or above (Please follow link to install and configure tomcat in eclipse: Configure and Run Tomcat server in eclipse)
After all set up and configuration you will see below generated XML sheet:
Please follow below steps:
- Create dynamic web project in eclipse name: GenerateXMLReport (Please use this link if you are not familiar how to create dynamic project in eclipse: Create Dynamic Web Project Eclipse)
- Once you create dynamic project it should look as below structure:
- Create package name com.javahonk inside src folder
- Create servlet class name ReportServlet.java inside com.javahonk package and copy paste below code:
package com.javahonk; import java.io.IOException; import java.util.ArrayList; import java.util.Iterator; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * @author Java Honk * Servlet implementation class ReportServlet */ @WebServlet("/ReportServlet") public class ReportServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public ReportServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, * HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/xml"); String reportName = "GenerateXML_Report_" +System.currentTimeMillis()+".xml"; response.setHeader("Content-disposition", "attachment; " + "filename=" + reportName); ArrayList<String> rows = new ArrayList<String>(); rows.add("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); rows.add("<Details>"); for (int i = 0; i < 4; i++) { int j = i+1; rows.add("<Person"+(i+1)+">"); rows.add("<FirstName>"); rows.add("Java"); rows.add("</FirstName>"); rows.add("<LastName>"); rows.add("Honk"); rows.add("</LastName>"); rows.add("<Test>"); rows.add("Success"); rows.add("</Test>"); rows.add("</Person"+(i+1)+">"); } rows.add("</Details>"); Iterator<String> iter = rows.iterator(); while (iter.hasNext()){ String outputString = (String) iter.next(); response.getOutputStream().print(outputString); } response.getOutputStream().flush(); } }
- Create index.jsp inside WebContent folder and copy paste below code:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Generate XML report</title> </head> <body> <form action="ReportServlet"> <input type="submit" value="Generate XML report"> </form> </body> </html>
- Final project structure will be as below:
- Now we are ready to generate XML using tomcat server. If you haven’t done tomcat set up in eclipse yet please use this link: Configure and Run Tomcat server in eclipse. Now right click project –>Run As –> Run on server.
- You will see below jsp page:
Click Generate XML report. Below pop-up will be Open, Save and Cancel button. For demo we have clicked Open button to open generated XML file:
- Once you clicked Open button finally you will see below generated XML file:
- That’s it dynamically generate XML using servlet created successfully.
Download GenerateXMLReport