Share on Google+Share on Google+

Create XML File using Servlet

In this section, you will learn how to create xml file using Servlet We have created file

Create XML File using Servlet


In this section, you will learn how to create xml file using Servlet  We have created  file It creates XML file with its version and encoding and display a message 'Xml File Created Successfully'.  

JAXP (Java API for XML Processing) is a Java interface that provides a standard approach to Parsing XML documents. With JAXP, we will use the Document BuilderFactory to create DocumentBuilder class. The class DocumentBuilderFactory is responsible for creating new DOM . 

DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = builderFactory .newDocumentBuilder();

DocumentBuilderFactory is used to find the class to load.

The instance of the class DocumentBuilder is used to create a blank document. 

Document doc = docBuilder.newDocument();

Step 1: Create a Servlet ""

import javax.servlet.*;
import javax.servlet.http.*;

import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;

public class XmlServlet extends HttpServlet{ 
  public void doGet(HttpServletRequest request, 
   HttpServletResponse response)
   throws ServletException,IOException{
  PrintWriter pw = response.getWriter();
  DocumentBuilderFactory builderFactory = 
  DocumentBuilder docBuilder = builderFactory.newDocumentBuilder();
  //creating a new instance of a DOM to build a DOM tree.
  Document doc = docBuilder.newDocument();
  new XmlServlet().createXmlTree(doc);
  pw.println("<b>Xml File Created Successfully</b>");
  catch(Exception e)

  public void createXmlTree(Document doc) throws Exception {
  //This method creates an element node
  Element root = doc.createElement("Company");
  //adding a node after the last child node of the specified node.

  Element child = doc.createElement("Location");

  Element child1 = doc.createElement("Companyname");

  Text text = doc.createTextNode("Roseindia .Net");

  Comment comment = doc.createComment("Employee in roseindia");

  Element element = doc.createElement("Employee");

  Text text1 = doc.createTextNode("Vineet Bansal");

  Element chilE = doc.createElement("Id");
  chilE.setAttribute("name", "Vineet");

  Text text12 = doc.createTextNode("status");
 //TransformerFactory instance is used to create Transformer objects. 
  TransformerFactory factory = TransformerFactory.newInstance();
  Transformer transformer = factory.newTransformer();
  transformer.setOutputProperty(OutputKeys.INDENT, "yes");

  // create string from xml tree
  StringWriter sw = new StringWriter();
  StreamResult result = new StreamResult(sw);
  DOMSource source = new DOMSource(doc);
  transformer.transform(source, result);
  String xmlString = sw.toString();

  File file = new File("c:/newxml.xml");
  BufferedWriter bw = new BufferedWriter
  (new OutputStreamWriter(new FileOutputStream(file)));


Step 2:Create a "Web.xml" for mapping the Servlet "". 

<?xml version="1.0" encoding="ISO-8859-1"?>

<web-app xmlns=""

<display-name>Welcome to Tomcat</display-name>
Welcome to Tomcat


Output :



Download the full source code



Posted on: December 14, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: Create XML File using Servlet   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
Rajesh Kumar
December 16, 2011

Very nice code, it works in one go