Dynamically generate XML using servlet

Dynamically generate XML using servlet

To dynamically generate XML using servlet below are needed:

After all set up and configuration you will see below generated XML sheet:

To dynamically generate XML using servlet below are needed:

 

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:

To dynamically generate XML using servlet below are needed:

 

  • 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:

To dynamically generate XML using servlet below are needed:

 

  • 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:

To dynamically generate XML using servlet below are needed:

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:

To dynamically generate XML using servlet below are needed:

 

  • Once you clicked Open button finally you will see below generated XML file:

To dynamically generate XML using servlet below are needed:

 

  • That’s it dynamically generate XML using servlet created successfully.

Dynamically generate XLS using servlet  Download GenerateXMLReport

 

Leave a Reply

Your email address will not be published. Required fields are marked *