How to store data entered by user in JSP page to be saved in XML file.On clicking submit button data entered by a user in a page must be saved in XML file.How to do so? Example: a user will enter empid,emp name ,email id on a particular page.How it will be saved in Emp.xml (I am using Jdeveloper) Please give steps in detail
JSP store data entered by user into XML file
1)form.jsp:
<html> <form method="post" action="createxml.jsp"> <table> <tr><td>Name:</td><td><input type="text" name="name"></td></tr> <tr><td>Address:</td><td><input type="text" name="address"></td></tr> <tr><td>Contact No:</td><td><input type="text" name="contact"></td></tr> <tr><td>Email:</td><td><input type="text" name="email"></td></tr> <tr><td></td><td><input type="submit" value="Submit"></td></tr> </table> </form> </html>
2)createxml.jsp:
<%@page import="java.io.*,org.w3c.dom.*,javax.xml.parsers.*,javax.xml.transform.*, javax.xml.transform.dom.*,javax.xml.transform.stream.*"%> <%! public void createXmlTree(Document doc,String name,String address,String contact,String email) throws Exception { System.out.println(name); Element root = doc.createElement("Employee"); doc.appendChild(root); Element child1 = doc.createElement("Name"); root.appendChild(child1); Text text1 = doc.createTextNode(name); child1.appendChild(text1); Element child2 = doc.createElement("Address"); root.appendChild(child2); Text text2 = doc.createTextNode(address); child2.appendChild(text2); Element child3 = doc.createElement("ContactNo"); root.appendChild(child3); Text text3 = doc.createTextNode(contact); child3.appendChild(text3); Element child4 = doc.createElement("Email"); root.appendChild(child4); Text text4 = doc.createTextNode(email); child4.appendChild(text4); TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); 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:/emp.xml"); BufferedWriter bw = new BufferedWriter(new FileWriter(file)); bw.write(xmlString); bw.flush(); bw.close(); }%> <% String name=request.getParameter("name"); String address=request.getParameter("address"); String contact=request.getParameter("contact"); String email=request.getParameter("email"); try{ System.out.println(name); DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = builderFactory.newDocumentBuilder(); Document doc = docBuilder.newDocument(); createXmlTree(doc,name,address,contact,email); out.println("<b>Xml File Created Successfully</b>"); } catch(Exception e) { System.out.println(e); } %>
Ads