I have made a form in jsp having emp id, projectname and emp name.I want to store data related to employee in xml file.How can i store data entered by user in XML file and later retrieve data in another jsp page using retrieve submit button.Please tell step by step instructions .I am using Jdeveloper.Thanx for ur help
JSP store for data into xml:
<%@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("Student"); 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:/new.xml"); BufferedWriter bw = new BufferedWriter(new FileWriter(file)); bw.write(xmlString); bw.flush(); bw.close(); }%> <% String name="roseindia"; String address="Delhi"; String contact="111111"; String email="[email protected]"; 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); } %>
Thank you sir for ur precious reply: xml file creates successfully,but i want data entered by user to be stored in xml file.here you have entered data in JSP file itself.what if i want to store data entered by user itself. Please take example to create one or two textboxes to enter data and store in xml. thanking you a lot
Ads