Stored Data in XML File using Servlet

In this section, you will learn how to stored data in xml file using Servlet We have created file login.jsp,

Stored Data in XML File using Servlet

Stored Data in XML File using Servlet


In this section, you will learn how to stored data in xml file using Servlet  We have created  file login.jsp, User details in the "login.jsp" are to be stored in xml 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 web page  "login.jsp"

<form name="loginform" method="post" action="xmlServlet">
<table align="center" style="border:1px solid #000000;" bgcolor="#efefef">
<td colspan=2 align="center" style="font-weight:bold;">UserLogin</td>

<td>User Name</td>
<td><input type="text" name="username" value=""></td>
<td><input type="password" name="password" value=""></td>

<td><input type="submit" name="Submit" value="Save"></td>

Step 2: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 doPost(HttpServletRequest request, 
  HttpServletResponse response
  throws ServletException,IOException{
  PrintWriter pw = response.getWriter();
  String username="";
  String password="";

&& request.getParameter("password")!=null)
 username = request.getParameter("username").toString();
 password = request.getParameter("password").toString();
  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,username,password);
  pw.println("<b>Xml File Created Successfully</b>");
  catch(Exception e)
  public void createXmlTree
   (Document doc,String userid,String password
throws Exception {
  //This method creates an element node
  Element root = doc.createElement("User");
  //adding a node after the last child node of the specified node.
  Element child1 = doc.createElement
  Text text = doc.createTextNode(userid);
Element element = doc.createElement("Password");
  Text text1 = doc.createTextNode(password);
 //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:/loginXml.xml");
  BufferedWriter bw = new BufferedWriter
 new OutputStreamWriter(new FileOutputStream(file)));

 Step 3:Create a  web.xml for mapping Servlet "". 

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

<web-app xmlns=""

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


Output :


After creating "loginXml.xml" successful message display.


Download the full source code