Tuesday, 21 October 2014

TransformerHandler : send XML from servlet


By using TransformerHandler you can create and send XML data to browser.

Step 1: Get the instance of SAXTransformerFactory.
Ex: SAXTransformerFactory stf = (SAXTransformerFactory) SAXTransformerFactory.newInstance();

Step 2: Get a TransformerHandler object.
Ex: TransformerHandler thd = stf.newTransformerHandler()

Step 3: Enable TransformerHandler to set the result for the transformation.
Ex:
PrintWriter writer = res.getWriter();
StreamResult streamResult = new StreamResult(writer);
thd.setResult(streamResult);

Step 4: Use TransformerHandler methods to create document, elements and attributes etc.,

import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.sax.TransformerHandler;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.Transformer;

@WebServlet(name="Employee", urlPatterns={"/Employee"})
public class Employee extends HttpServlet {
    static TransformerHandler getHandler() throws TransformerConfigurationException{
        SAXTransformerFactory stf = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
        TransformerHandler thd = stf.newTransformerHandler();
        Transformer serializer = thd.getTransformer();
        serializer.setOutputProperty(OutputKeys.INDENT, "yes");
        return thd;
    }

    @Override
   public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException{
       TransformerHandler thd = null;
        try {
            thd = getHandler();
        } catch (TransformerConfigurationException ex) {
            Logger.getLogger(Employee.class.getName()).log(Level.SEVERE, null, ex);
        }
        String id[] = {"1","2","3","4"};
        String name[] = {"Krishna", "Murali", "Susantha", "Shanmugham"};

        PrintWriter writer = res.getWriter();
        StreamResult streamResult = new StreamResult(writer);

        thd.setResult(streamResult);
        try{
            thd.startDocument();
            thd.startElement(null, null, "employees", null);

            for(int i=0; i<id.length; i++){
                thd.startElement("", "", "employee", null);

                thd.startElement("", "", "name", null);
                thd.characters(name[i].toCharArray(), 0, name[i].length());
                thd.endElement("", "", "name");

                thd.startElement("", "", "id", null);
                thd.characters(id[i].toCharArray(), 0, id[i].length());
                thd.endElement("", "", "id");

                thd.endElement("", "", "employee");
            }

            thd.endElement(null, null, "employees");
            thd.endDocument();
        }
        catch(Exception e){

        }
   }
}

Output




 
Prevoius                                                 Next                                                 Home

No comments:

Post a Comment