Jump to content

[solved]JavaScript XML search tagName


karthikin

Recommended Posts

Hi Friends,(I'm not really good at programming but still i'm learning to understand and do it.)How can i search for a tagName in XML file using JavaScript if a match found then store all the values in an array and display the output.In my xml file <CONTACT> is the parent node and inside i have a <PDE-Identity>N65553</PDE-Identity> when a user type the <PDE-Identity>value in the search box and click on search it should match the corresponding PDE-Identity value in the contact and display the output in the corresponding text boxes.I'll be grateful for any help offered! Thanks guys.My xml file:

<?xml version="1.0" encoding="UTF-8"?><CONTACTS><CONTACT><PDE-Identity>N65539</PDE-Identity><FirstName>Arun_niit</FirstName><LastName>Arun_niit</LastName><gmail/><yahoo>nura_poping@yahoo.co.in</yahoo><alcatel-lucent/><URL/><Facebook-ID/></CONTACT><CONTACT><PDE-Identity>N65593</PDE-Identity><FirstName>Rangarajkarthik</FirstName><LastName>karthik Rangaraj</LastName><gmail>kart2006@gmail.com</gmail><yahoo>karthikrangaraj@yahoo.com</yahoo><alcatel-lucent/><URL/><Facebook-ID/></CONTACT></CONTACTS>

This is my java script:

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("file://D:/xmlparserinxul/finalversion2.xml");    }// MSIE uses ActiveX    else if (window.ActiveXObject) {        xmlDoc = new ActiveXObject("Microsoft.XMLDOM");        xmlDoc.async = "false";        xmlDoc.load("file://D:/xmlparserinxul/finalversion2.xml");    }}function searchIndex() { // search the index (duh!)    if (!xmlDoc) {        loadIndex();    }    // get the search term from a form field with id 'searchme'    var searchterm = document.getElementById("find").value;    var x = xmlDoc.getElementsByTagName("CONTACT");    var allitems = xmlDoc.getElementsByTagName("PDE-Identity");    results = new Array;    if (searchterm.length < 6) {        alert("Please enter 6 characters, PDE starts with 'N' followed by 5 numbers");    } else {        for (var i=0;i<allitems.length;i++) {// see if the XML entry matches the search term,// and (if so) store it in an array                   }// send the results to another function that displays them to the user    showResults();    }}function showResults() {var str = get_node_value(x.getElementsByTagName("PDE-Identity")[0] );document.getElementById("PDE-Identity").value=str; var str = get_node_value(x.getElementsByTagName("FirstName")[0] );document.getElementById("FirstName").value=str;  var str = get_node_value(x.getElementsByTagName("LastName")[0] );document.getElementById("LastName").value=str;  var str = get_node_value(x.getElementsByTagName("gmail")[0] );document.getElementById("gmail").value=str;  var str = get_node_value(x.getElementsByTagName("yahoo")[0] );document.getElementById("yahoo").value=str;  var str = get_node_value(x.getElementsByTagName("alcatel-lucent")[0] );document.getElementById("alcatel-lucent").value=str;      var str = get_node_value(x.getElementsByTagName("URL")[0] );document.getElementById("URL").value=str;   var str = get_node_value(x.getElementsByTagName("Facebook-ID")[0] );document.getElementById("Facebook-ID").value=str;}function get_node_value (element) {   if (element.childNodes.length > 0) {      return element.childNodes[0].nodeValue;   }   else {      return "";   }}function clear(){document.getElementById("find").value='';}

My text boxes in XUL :

<label value="Type PDE-ID to Search"/>      <textbox id="find" type="search"/>       <button label="Search" oncommand="searchIndex();"/>       <button label="Clear" oncommand="clear();"/>           </hbox><row>        <label value="PDE-Identity"/>        <textbox id="PDE-Identity" value=""/>      </row>      <row>        <label value="FirstName"/>        <textbox id="FirstName" value=""/>      </row>      <row>        <label value="LastName"/>        <textbox id="LastName" value=""/>      </row>      <row>          <label value="Gmail"/>        <textbox id="gmail" value=""/>      </row>      <row>          <label value="Yahoo"/>        <textbox id="yahoo" value=""/>      </row>      <row>          <label value="Alcatel-Lucent"/>        <textbox id="alcatel-lucent" value="" />      </row>      <row>          <label value="URL"/>        <textbox id="URL" value="" />      </row>      <row>          <label value="Facebook-ID"/>        <textbox id="Facebook-ID" value="" />      </row>

Link to comment
Share on other sites

For each contact, check if the PDE-Identity element is equal to the value you're looking for.When you find the right one, take all the data belonging to that contact element.

Link to comment
Share on other sites

For each contact, check if the PDE-Identity element is equal to the value you're looking for.When you find the right one, take all the data belonging to that contact element.
You are right! Could you please explain to me how could I check for PDE-Identity element in each contact and get the remaining values.I have a for loop to check for the number of characters of the search item how could I proceed further?Could you explain to me in detail with some code. Thank you very much!
Link to comment
Share on other sites

That's what I just explained. I know that if I just give you some code you're going to copy it without thinking about how it works.

  1. Use getElementsByTagName() to get a list of all the <contact> elements in the document.
  2. Then, loop through them with a for loop.
  3. On each iteration, use getElementsByTagName() to obtain a list of <PDE-Identity> elements and select the first one (index 0 of the node list).
  4. Compare the value of that element with the value you're looking for. The value of the element would be the node value of the first child (text node) of that element. element.firstChild.nodeValue
  5. if() the value is equal, then use getElementsByTagName() to obtain the nodelists of each type of element you want and take the first element of each node list (index 0). With each of these elements, get the value of the text node within them using firstChild.nodeValue.

Link to comment
Share on other sites

That's what I just explained. I know that if I just give you some code you're going to copy it without thinking about how it works.
  1. Use getElementsByTagName() to get a list of all the <contact> elements in the document.
  2. Then, loop through them with a for loop.
  3. On each iteration, use getElementsByTagName() to obtain a list of <PDE-Identity> elements and select the first one (index 0 of the node list).
  4. Compare the value of that element with the value you're looking for. The value of the element would be the node value of the first child (text node) of that element. element.firstChild.nodeValue
  5. if() the value is equal, then use getElementsByTagName() to obtain the nodelists of each type of element you want and take the first element of each node list (index 0). With each of these elements, get the value of the text node within them using firstChild.nodeValue.

Thank you very much!!
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...