ksmadhuri 0 Newbie Poster

hi,
i had done some coding ..im unable to get how to check the entered field is present in xml file or not...can anyone help me out....waiting for the reply..here is the code..

1. package login;
2.
3. import java.io.*;
4. import javax.servlet.*;
5. import javax.servlet.http.*;
6.
7. import java.io.*;
8. import org.w3c.dom.*;
9. import javax.xml.parsers.*;
10. import javax.xml.transform.*;
11. import javax.xml.transform.dom.*;
12. import javax.xml.transform.stream.*;
13.
14. public class register extends HttpServlet
15. {
16. public void doPost(HttpServletRequest request, HttpServletResponse response)
17. throws ServletException, IOException
18. {
19. response.setContentType("text/html");
20. PrintWriter pw = response.getWriter();
21. String id = "";
22. String name = "";
23. String gender = "";
24. String qual = "";
25. id = request.getParameter("id");
26. name = request.getParameter("name");
27. gender = request.getParameter("gender");
28. qual = request.getParameter("qual");
29. try
30. {
31. DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
32. DocumentBuilder docBuilder = builderFactory.newDocumentBuilder();
33. java.io.File file = new java.io.File("E:/login.xml");
34. Document doc = null;
35. if (file.exists()) {
36. doc = docBuilder.parse(file);
37. } else {
38. doc = docBuilder.newDocument();
39. Element root = doc.createElement("LOGIN");
40. doc.appendChild(root);
41. }
42. createXmlTree(doc, id, name, gender, qual);
43. }
44. catch (Exception e) {
45. System.out.println(e);
46. }
47. }
48.
49. private void createXmlTree(Document doc, String id, String name, String gender, String qual)
50. throws Exception {
51. Node node = doc.getFirstChild();
52. Node childnode =doc.getChildNodes().item(1);
53. Element child = doc.createElement("LIST");
54. Element subchild = doc.createElement("GENERAL");
55. // adding a node after the last child node of the specified node.
56. node.appendChild(child);
57. child.appendChild(subchild);
58.
59. Element child1 = doc.createElement("ID");
60. subchild.appendChild(child1);
61. Text text = doc.createTextNode(id);
62. child1.appendChild(text);
63.
64. Element element = doc.createElement("NAME");
65. subchild.appendChild(element);
66. Text text1 = doc.createTextNode(name);
67. element.appendChild(text1);
68.
69. Element element1 = doc.createElement("GENDER");
70. subchild.appendChild(element1);
71. Text text2 = doc.createTextNode(gender);
72. element1.appendChild(text2);
73.
74. Element element2 = doc.createElement("QUAL");
75. subchild.appendChild(element2);
76. Text text3 = doc.createTextNode(qual);
77. element2.appendChild(text3);
78.
79. // TransformerFactory instance is used to create Transformer objects.
80. TransformerFactory factory = TransformerFactory.newInstance();
81. Transformer transformer = factory.newTransformer();
82. transformer.setOutputProperty(OutputKeys.INDENT, "yes");
83. // create string from xml tree
84. StringWriter sw = new StringWriter();
85. StreamResult result = new StreamResult(sw);
86. DOMSource source = new DOMSource(doc);
87. transformer.transform(source, result);
88. String xmlString = sw.toString();
89.
90. File file = new File("E:/login.xml");
91. BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
92. new FileOutputStream(file)));
93. bw.write(xmlString);
94. bw.flush();
95. bw.close();
96.
97. }
98. }

Thanks in advance,
harini.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.