1 Replies - 786 Views - Last Post: 30 December 2013 - 10:26 PM

#1 Pearl08  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 23-October 13

how to retrieve the data from HTML in servlet and make an xml?

Posted 30 December 2013 - 10:24 PM

Hello All,

I've written the following code for the HTML Page :
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<form action="" method="post">
EmpNo :<input type="text" name="empno"/><br/>
EmpName :<input type="text" name="empname"/><br/>
Job :<input type="text" name="job"/><br/>
ManagerName :<input type="text" name="mgrname"/><br/>
HireDate :<input type="text" name="hrdate"/><br/>
Salary :<input type="text" name="sal"/><br/>
Commission :<input type="text" name="comm"/><br/>
DeptNo :<input type="text" name="deptno"/><br/>

<input type="submit" value="Submit" />

And Now I've to make a servlet which will retrieve the data from the HTML and will make an XML File...
I've written the following code for servlet:

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;  

public class XmlServlet extends HttpServlet{ 
  public void doGet(HttpServletRequest request,HttpServletResponse response)
   throws ServletException,IOException{
  PrintWriter pw = response.getWriter();
  DocumentBuilderFactory builderFactory =DocumentBuilderFactory.newInstance();
  DocumentBuilder docBuilder = builderFactory.newDocumentBuilder();
  //creating a new instance of a DOM to build a DOM tree.
  Document doc = docBuilder.newDocument();
  Element root = doc.createElement("Company");
  //adding a node after the last child node of the specified node.

  Element child = doc.createElement(request.getParameter("empno"));
  //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:/Users/N/Desktop/file1.xml");
  BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));
  pw.println("<b>Xml File Created Successfully</b>");
  catch(Exception e)


Thank you

This post has been edited by macosxnerd101: 30 December 2013 - 10:25 PM
Reason for edit:: Please use code tags

Is This A Good Question/Topic? 0
  • +

Replies To: how to retrieve the data from HTML in servlet and make an xml?

#2 macosxnerd101  Icon User is offline

  • Games, Graphs, and Auctions
  • member icon

Reputation: 12241
  • View blog
  • Posts: 45,327
  • Joined: 27-December 08

Re: how to retrieve the data from HTML in servlet and make an xml?

Posted 30 December 2013 - 10:26 PM

Welcome to Dream.in.Code!

I am moving this thread to the Java EE forum, as Servlets fall under the Java EE umbrella. Also, please remember to use code tags.

In order for our members to better assist you, please remember to include a clear and specific description of any problems or errors you are encountering.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1