package com.phani.xml; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.File; import java.io.FileWriter; import java.io.PrintWriter; import java.io.IOException; import org.dom4j.io.SAXReader; import org.dom4j.Document; import org.dom4j.Element; import org.dom4j.io.XMLWriter; import org.dom4j.io.OutputFormat; public class ReadXMLInputStreamServlet extends HttpServlet { private SAXReader xmlReader; private Document document; private XMLWriter writer; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { xmlReader = new SAXReader(); try { //read input stream and build a document node document = xmlReader.read(request.getInputStream()); //write the newly build document to a file writer = new XMLWriter(new FileWriter( new File("C:/stream.xml") )); writer.write(this.document); writer.close(); response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println(""); out.println("
"); out.println("