Venkat S

Venkat S

  • 1.5k
  • 234
  • 203.1k

How to searchTextBox entered value in XML File using javascript XMLHTTP

Jul 14 2011 9:32 AM
Hi friends
Please resolve my problom
My xml file
<searchable_index> 
   <Search>
     <keyword>101</keyword>
     <link>R-12</link>
   <color>white</color>
  </Search>

  <Search>
     <keyword>102</keyword>
     <link>R-22</link>
   <color>white</color>
  </Search>
<Search>
     <keyword>101</keyword>
     <link>R-12</link>
   <color>white</color>
  </Search>

  <Search>
     <keyword>102</keyword>
     <link>R-22</link>
   <color>white</color>
  </Search>
<Search>
     <keyword>101</keyword>
     <link>R-12</link>
   <color>white</color>
  </Search>

  <Search>
     <keyword>102</keyword>
     <link>R-22</link>
     <color>white</color>
  </Search>
</searchable_index>
///////HTML code
<html>
<script language="javascript" type="text/javascript">
  window.onload = loadIndex;

  function loadIndex() { // load indexfile
  // most current browsers support document.implementation
  if (document.implementation && document.implementation.createDocument) {
  xmlDoc = document.implementation.createDocument("", "", null);
  xmlDoc.load("index.xml");
  }
  // MSIE uses ActiveX
  else if (window.ActiveXObject) {
  xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
  xmlDoc.async = "false";
  xmlDoc.load("index.xml");
  }
  }

  var x;
  var y = 0;
   

  function searchIndex()
  { // search the index (duh!)
  if (!xmlDoc)
  {
  loadIndex();
  }
  // get the search term from a form field with id 'searchme'
  results = new Array;
  var searchterm = document.getElementById("searchme").value;
  x = xmlDoc.getElementsByTagName("Search");
   
  for (i = 0; i < x.length; i++)
  {   
  if (searchterm.length < 1)
  {
  alert("Enter at least three characters");
  }
  else
  {   
  // see if the XML entry matches the search term,
  // and (if so) store it in an array
  var name = x[i].childNodes[1].firstChild.nodeValue;
  //var lnkname = x[i].childNodes[0].firstChild.nodeValue;
  //alert(lnkname);
  var exp = new RegExp(searchterm, "i");
       
  if (name.match(exp) != null)
       {
  y = y + 1;   
         
  results.push(name);
       
  test(name);
       
  }
  }
  }  
       
  }

  function test(name, lnkname)
  {
   
  document.write('<table border="0" style="width:100%">');
  for (var i = 0; i < x.length; i++)
  {   
     //var sear=x[i].childNodes[1].firstChild.nodeValue + x[i].childNodes[0].firstChild.nodeValue;   
  document.write("<tr onclick='displayInfo(" + i + ")'>");   
  document.write('<td>' + x[i].childNodes[1].firstChild.nodeValue + '</td>');
  document.write('</tr>');
  }
     //document.getElementById("show").innerHTML = sear;
  document.write('<table>');
  document.close();
  }   
   

  //
  </script>

</head>
<body>
  <input id="searchme" type="text" size="20">&nbsp;&nbsp;<input type="button" value="Search" onclick="searchIndex(); return false;" />

<input id="get" type="text" />
<div id="show"></div>
</body>
</html>
i tryed this but i dont want to use XMLDOM
i want to use XMLHTTP
any one knows please modify my code and send me otherwise
give me any idea how to do.
i hope sombady help thanks advanced
Thanks
Venkat.S

Answers (2)