hello i was appending my XML file to add more data entered by user in JSP page.But not getting XML file in proper format as XML rules.lease help me to get XML file in proper format.I am using Jdeveloper. My code:
<%@page import="java.io.*,org.w3c.dom.*,javax.xml.parsers.*,javax.xml.transform.*, javax.xml.transform.dom.*,javax.xml.transform.stream.*,javax.xml.*"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=windows-1252"> <title>untitled</title> </head> <body> <P> <form> <P>Name <input type="text" name="text1"/> </P> <P>Address <input type="text" name="text2"/></P> <P>Contact <input type="text" name="text3"/></P> <P>Email <input type="text" name="text4"/></P> <P> </P> <P> <input type="submit" value="Submit" name="submit"/> </P> </form></P> <%! public void createXmlTree(String name,String address,String contact,String email) throws Exception { Element root; DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = builderFactory.newDocumentBuilder(); Document doc = docBuilder.newDocument(); File file = new File("c:/new.xml"); if (file.exists()) { //DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance(); //DocumentBuilder builder = fact.newDocumentBuilder(); doc = docBuilder.parse(file); root = doc.getDocumentElement(); String sr = root.getNodeName(); //root = node.getNodeName(); } else { System.out.println(name); root = doc.createElement("Students"); doc.appendChild(root); } Element child = doc.createElement("Student"); root.appendChild(child); Element child1 = doc.createElement("Name"); child.appendChild(child1); Text text1 = doc.createTextNode(name); child1.appendChild(text1); Element child2 = doc.createElement("Address"); child.appendChild(child2); Text text2 = doc.createTextNode(address); child2.appendChild(text2); Element child3 = doc.createElement("ContactNo"); child.appendChild(child3); Text text3 = doc.createTextNode(contact); child3.appendChild(text3); Element child4 = doc.createElement("Email"); child.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(); FileWriter fw=new FileWriter(file,true); BufferedWriter bw = new BufferedWriter(fw); bw.write(xmlString); bw.flush(); bw.close(); }%> <% String name1,address1,contact1,email1; name1 = request.getParameter("text1"); address1 = request.getParameter("text2"); contact1 = request.getParameter("text3"); email1 = request.getParameter("text4"); String name=name1; String address=address1; String contact=contact1; String email=email1; try { //System.out.println(name); // DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); //DocumentBuilder docBuilder = builderFactory.newDocumentBuilder(); // Document doc = docBuilder.newDocument(); createXmlTree(name,address,contact,email); out.println("<b>Xml File Created Successfully</b>"); } catch(Exception e) { System.out.println(e); } %> </body> </html>
XMl file creates successfully at first instance but unable to append data entered bt more users in proper XML format. I have also tried following code but that also not in proper format: please provide proper code for forming proper XML file:
<%@page import="java.io.*,org.w3c.dom.*,javax.xml.parsers.*,javax.xml.transform.*, javax.xml.transform.dom.*,javax.xml.transform.stream.*,javax.xml.*"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=windows-1252"> <title>untitled</title> </head> <body> <P> <form> <P>Name <input type="text" name="text1"/> </P> <P>Address <input type="text" name="text2"/></P> <P>Contact <input type="text" name="text3"/></P> <P>Email <input type="text" name="text4"/></P> <P> </P> <P> <input type="submit" value="Submit" name="submit"/> </P> </form></P> <%! public void createXmlTree(String name,String address,String contact,String email) throws Exception { DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = builderFactory.newDocumentBuilder(); Document doc = docBuilder.newDocument(); 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:/emp.xml"); FileWriter fw=new FileWriter(file,true); BufferedWriter bw = new BufferedWriter(fw); bw.write(xmlString); bw.flush(); bw.close(); }%> <% String name1,address1,contact1,email1; name1 = request.getParameter("text1"); address1 = request.getParameter("text2"); contact1 = request.getParameter("text3"); email1 = request.getParameter("text4"); String name=name1; String address=address1; String contact=contact1; String email=email1; try { System.out.println(name); DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = builderFactory.newDocumentBuilder(); Document doc = docBuilder.newDocument(); createXmlTree(name,address,contact,email); out.println("<b>Xml File Created Successfully</b>"); } catch(Exception e) { System.out.println(e); } %> </body> </html>
The code is working .. it append the record with the root signature of xml that's "". i dont want with every record.. Kindly help me in this.. i tried a lot. Thank you
Thanks, it is very good
Ads