469,315 Members | 1,802 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,315 developers. It's quick & easy.

how to check the entered field exists are not in xml

149 100+
hi,
i m new to xml..in my project..i need to develop xml file..i had done it...i used jsp pages for entering the values in xml file...if i enter the same value which is present in xml file...it should throw an alert that 'Already the name exists'...can anyone help me out..i used DOM for creation of XML file...

Thanks in advance..
madhu.
Jun 22 '10 #1
13 4697
jkmyoung
2,057 Expert 2GB
What is the structure of your xml like? Are these values stored in particular elements? Can you get a list of these elements, eg using some sort of getElementsByTagName function? or SelectNodes?
Jun 22 '10 #2
madhuriks
149 100+
hi,
here is my XML file...i entered all the values using jsp page..i.e P_Name,M_Status..etc...if i re-enter the values..it should show me an alert that the name already exists..


Expand|Select|Wrap|Line Numbers
  1. <PD>
  2. <GT>
  3. <GL>
  4. <P_Name>Hello</P_Name>
  5. <M_Status>M</M_Status>
  6. <E_Qualification>B.Tech</E_Qualification>
  7. </GL>
  8. </GT>
  9. </PD>
Thanks in Advance,
madhu.
Jun 22 '10 #3
madhuriks
149 100+
hi,
i created xml file using DOM...if i enter the values...which are present in xml file...it is not showing any error...it is accepting the value...can anyone suggest me how to check the enter value with existing value...i need help from anyone urgently...

Thanks in advance,
madhu.
Jun 23 '10 #4
Dormilich
8,651 Expert Mod 8TB
I would need to see some code to make a sensible statement.
Jun 23 '10 #5
madhuriks
149 100+
hi,
this is my code waiting for the reply..

Expand|Select|Wrap|Line Numbers
  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. }
this is my code..can u chk it nd do me the needful..

Thanks in advance,
madhu.
Jun 23 '10 #6
jkmyoung
2,057 Expert 2GB
"...if i enter the same value"
How exactly are you entering these values?

What type of DOM?

It looks like you can just search your P_Name nodes every time you make an entry.
Jun 23 '10 #7
jkmyoung
2,057 Expert 2GB
As you add the nodes, create a vector to hold the already used values. When you try to add new values, compare against the values in the vector.
Jun 23 '10 #8
madhuriks
149 100+
@jkmyoung
hi,
if i enter the 'P_NAME' value as 'Hello' again it is taking ...how to solve the prob.

Thanks,
madhu.
Jun 24 '10 #9
jkmyoung
2,057 Expert 2GB
Did you try what we suggested? Please show us the code for your try.
Jun 24 '10 #10
madhuriks
149 100+
hi,
i had done by using Ajax..it is validating..the problem is if i entering the values it is not taking...can i get any suggestion what is the problem in the code...here is the code...waiting for the reply..

Expand|Select|Wrap|Line Numbers
  1. function validate() {
  2.     if (document.frm.id.value == "") {
  3.         alert("Enter Package Name");
  4.         document.frm.id.focus();
  5.         return (false);
  6.     }
  7.     chkId = /^[WAP_]{4}\d{2}$/;
  8.     if (chkId.test(document.frm.id.value)) {
  9.     } else {
  10.         alert('Invalid \"Pack Name\" Entry');
  11.         return false;
  12.     }
  13.     if (document.frm.act.value == "") {
  14.         alert("Enter Activation Status");
  15.         document.frm.act.focus();
  16.         return (false);
  17.     }
  18.     chkAct = /^[MA]{2}\d{2}$/;
  19.     if (chkAct.test(document.frm.act.value)) {
  20.     } else {
  21.         alert('Invalid \"Activation Status\" Entry');
  22.         return false;
  23.     }
  24.     if (document.frm.deact.value == "") {
  25.         alert("Enter De-Activation Status");
  26.         document.frm.deact.focus();
  27.         return (false);
  28.     }
  29.     chkDeact = /^[MD]{2}\d{2}$/;
  30.     if (chkDeact.test(document.frm.deact.value)) {
  31.     } else {
  32.         alert('Invalid \"De-Activation Status\" Entry');
  33.         return false;
  34.     }
  35.     if (document.frm.allwpp.selectedIndex < 0) {
  36.         alert("Select one of the \"Allow Post-Paid or Not\" options.");
  37.         document.frm.allwpp.focus();
  38.         return (false);
  39.     }
  40.     if (document.frm.allwpp.selectedIndex == 0) {
  41.         alert("Select \"Allow Post-Paid or Not\" option.");
  42.         document.frm.allwpp.focus();
  43.         return (false);
  44.     }
  45.     if (document.frm.shrt.value == "") {
  46.         alert("Enter Package Name");
  47.         document.frm.shrt.focus();
  48.         return (false);
  49.     }
  50.     return true;
  51. }
  52. function trim(stringToTrim) {
  53.     return stringToTrim.replace(/^\s+|\s+$/g, "");
  54. }
  55. var xmlHttp = null;
  56. function login() {
  57.     if (validate()) {
  58.  
  59.         try {
  60.             // Firefox, Opera 8.0+, Safari
  61.             xmlHttp = new XMLHttpRequest();
  62.         } catch (e) {
  63.             // Internet Explorer
  64.             try {
  65.                 xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
  66.             } catch (e) {
  67.                 xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  68.             }
  69.         }
  70.         var id = document.getElementById("id").value;
  71.         var url = "/xmlgprs/login.xml";
  72.         url = url + "?id=" + id;
  73.         xmlHttp.onreadystatechange = stateChanged;
  74.         xmlHttp.open("GET", url, true);
  75.         xmlHttp.send(null);
  76.     }
  77. }
  78. function stateChanged() {
  79.     if (xmlHttp.readyState == 4) {
  80.         if (xmlHttp.status == 200) {
  81.             parseMsg();
  82.         } else {
  83.             alert("Unable to retrieve");
  84.         }
  85.     }
  86. }
  87. function trim(stringToTrim)
  88. {
  89.     return stringToTrim.replace(/^\s+|\s+$/g,"");
  90. }
  91. function parseMsg() {
  92.     var id = document.getElementById("id").value;
  93.         response = xmlHttp.responseXML.documentElement;
  94.         var node = response.getElementsByTagName("PACK_NAME");
  95.         //Na=Node();
  96.         //Na.toString()
  97.     //    alert(node.length)
  98.         //ele=Element()
  99.  
  100.         //ele.nodeValue
  101.         for ( var i = 0; i < node.length; i++) {
  102.             var node1 = node.item(i);
  103.             var childNodes=node1.childNodes;
  104.             for ( var j = 0; j < childNodes.length; j++) {
  105.                 var childNode=childNodes.item(j);
  106.                 if(childNode.nodeType == Node.TEXT_NODE){
  107.                     alert(childNode.nodeValue);
  108.                     if(trim(childNode.nodeValue) == trim(id)){
  109.                         alert("already Present");
  110.                         break;
  111.                     }
  112.                 }
  113.             }
  114.  
  115.             //idArray[i] = node1.text;
  116.         }
  117.  
  118.  
  119. }

Thanks in advance,
madhu.
Jun 25 '10 #11
jkmyoung
2,057 Expert 2GB
Expand|Select|Wrap|Line Numbers
  1.                     if(trim(childNode.nodeValue) == trim(id)){ 
  2.                         alert("already Present"); 
  3.                         break; 
  4.                     } 
  5.  
In this code, all you do is break. It doesn't stop the node from being added. I suggest setting a flag in this case; check if the flag is set before you add the node.
Jun 25 '10 #12
madhuriks
149 100+
@jkmyoung
hi,
i had done...it is getting added..can u suggest me where is the problem...here is the code...

Expand|Select|Wrap|Line Numbers
  1. function parseMsg() {
  2.     var id = document.getElementById("id").value;
  3.     response = xmlHttp.responseXML.documentElement;
  4.     var node = response.getElementsByTagName("PACK_NAME");
  5.     for ( var i = 0; i < node.length; i++) {
  6.         var node1 = node.item(i);
  7.         var childNodes = node1.childNodes;
  8.         for ( var j = 0; j < childNodes.length; j++) {
  9.             var childNode = childNodes.item(j);
  10.             if (childNode.nodeType == Node.TEXT_NODE) {
  11.                 //alert(childNode.nodeValue);
  12.                 if (id == childNode.nodeValue) {
  13.                     alert("Pack Already Exists");
  14.                     return true;
  15.                 }
  16.             }
  17.         }
  18.     }
Thanks in advance...
waiting for the reply,
madhu.
Jun 29 '10 #13
@madhuriks
Hi,

I am trying to do something similar, so may have a solution for you.

this is my code, which shows the alert when the entry is found in the xml.

Expand|Select|Wrap|Line Numbers
  1.     if (window.XMLHttpRequest)
  2.     {// code for IE7+, Firefox, Chrome, Opera, Safari
  3.         xmlhttp=new XMLHttpRequest();
  4.     }
  5.     else
  6.     {// code for IE6, IE5-
  7.         xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  8.     }
  9.     xmlhttp.open("GET","Products.xml",false);
  10.     xmlhttp.send();
  11.     xmlDoc=xmlhttp.responseXML;
  12.     var z=xmlDoc.getElementsByTagName("PRODUCT");
  13.  
  14.     function CheckProduct(){
  15.         var checkthis=document.getElementById("ProductNo").value;
  16.  
  17.         for(i=0; i<z.length; i++){
  18.             var filevalue=z[i].getElementsByTagName("PRODUCT_NO")[0].childNodes[0].nodeValue;
  19.             if(checkthis==filevalue){
  20.                 alert("This already exists");
  21.             }
  22.         }
  23.  
  24.     }
Oct 18 '11 #14

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Pawel Wrobel | last post: by
8 posts views Thread by News Microsoft | last post: by
1 post views Thread by Edwina Rothschild | last post: by
1 post views Thread by Agnes | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.