3 Replies - 1348 Views - Last Post: 05 November 2012 - 12:56 PM

#1 shelli06  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 05-November 12

Code Not working for search function

Posted 05 November 2012 - 12:28 PM

Hello!

This is my xml file:
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/css" href="CSSUSER.css"?>
<USERS>
   <user>
      <userid>Dessert King</userid>
	  <firstname>Rebecca</firstname>
	  <lastname>Fling</lastname>
	  <Specialty>Making pastries</Specialty>
	</user>
	<user>
		<userid>Screaming Fire</userid>
		<firstname>Don</firstname> 
		<lastname>Strove</lastname>
		<Specialty>Sautéing vegetables</Specialty>
	</user>
	<user>
		<userid>Stove Top</userid>
		<firstname>Isabella</firstname>
		<lastname>Bryonex</lastname>
		<Specialty>Ordering Take out</Specialty>
	</user>
	<user>
		<userid>Baking Chef</userid>
		<firstname>Baked</firstname>
		<lastname>Alaska</lastname>
		<Specialty>Baking breads from scratch</Specialty>
	</user>
	<user>
		<userid>BEBEQ</userid>
		<firstname>Bea</firstname>
		<lastname>Que</lastname>
		<Specialty>Barbeque, Cajun Style</Specialty>
	</user>
</USERS>



I created a search database page for my xml file for my website. I am not sure if I inputted my root, child, and child nodes in the correct spot. I am not getting errors when testing it out on the server. It is simply not searching.

<HTML>
<HEAD>
<script>
  var xmlDoc = new ActiveXObject("Msxml2.DOMdocument.3.0");
  xmlDoc.load("userid.xml");

function getObj(elementID)
{
	return document.getElementById(elementID);
}
function showForm()
{ 
  f = getObj("form1");
  f.style.visibility = "visible";
}

function showData()
{
  d = getObj("data");
  f = getObj("form1");
  var rootElem = xmlDoc.documentElement;
  var match = linearSearch(f, rootElem);
  if (match == -1)
  {
    alert("Sorry, no match");
    showForm();
    return;
  }
  var USERS = rootElem.childNodes.item(match);
  makeNodes(USERS, d);
  f.style.visibility = "hidden";
  d.style.visibility = "visible";
}

function linearSearch(f, rootElem)
{
  var searchuserid = f.userid.value;
  for (i = 0; i < rootElem.childNodes.length; i++)
  {
    var user = rootElem.childNodes.item(i);
    var thisuserid = "";
    for (j = 0; j <user.childNodes.length; j++)
    {
      var nodeText = user.childNodes.item(j).text;
      var nodeName = user.childNodes.item(j).nodeName;
      if (nodeName == "userid") thisuserid = nodeText;
    }
    if (thisuserid == searchuserid)
      return i;
  }
  return -1;
}

function makeNodes(user, d)
{
  emptyNode(d);
  for (i = 0; i <user.childNodes.length; i++)
  {
     var nodeText = user.childNodes.item(i).text;
     var nodeName = user.childNodes.item(i).nodeName;
     d.appendChild(document.createTextNode(nodeName + ": " + nodeText));
     d.appendChild(document.createElement("p"));
  }
  d.appendChild(document.createElement("<input type='button' " +
                                       "value='Do Another Search' " +
                                       "onclick='hideData();" +
                                       "showForm()'>"));

}
function hideData()
{
  d = getObj("data");
  d.style.visibility = "hidden";
}
function getObj(elementID)
{
	return document.getElementById(elementID);
}
function emptyNode(theNode)
{
	for (i= theNode.childNodes.length - 1; i >=0; i--)
	{
		theNode.removeChild(theNode.childNodes[i]);
	}
}
</SCRIPT>
<style type="text/css">
#form1 { position:absolute; left:0; top:0}
#data { position:absolute; left:0; top:0}
</style>
</HEAD>
<BODY onload="showForm();hideData()">
<FORM id="form1">
Enter customer number: <INPUT TYPE="text" name="userid">
<BR><INPUT TYPE="button" VALUE="Find Information" 
           onclick='showData();'>
</FORM>
<DIV id="data">
<INPUT TYPE="button" VALUE="Do another search" 
           onclick='hideData();showForm();'>
</DIV>

</BODY>
</HTML>


I am fairly new to xml coding where as javascript not so much. I am slightly confused as to why the actual code while I believe my root (users), child (user) and node (userid) is in the right place.

I want the search to result in being able to bring up the userid tag from the xml file for individuals searching the database.

Any ideas?

Michele

Thank you

This post has been edited by JackOfAllTrades: 05 November 2012 - 12:54 PM
Reason for edit:: Fixed code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Code Not working for search function

#2 Kruithne  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 99
  • View blog
  • Posts: 442
  • Joined: 28-July 09

Re: Code Not working for search function

Posted 05 November 2012 - 12:41 PM

Hello,

First, you missed a closing code tag, might want to fix that up.

You create a new ActiveXObject, this is only for Internet Explorer, no other browsers. I'm not sure if you intended it to be this way. Below is an example of how to grab data from an XML file from all browsers.

// Check of XMLHttpRequest is supported, if so we use it, else resort to IE ActiveX.
var request = (window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP"));

// Hook an event for when the request is ready.
request.onreadystatechange = function()
{
	// If the readyState is 4 and the status is 200 OK then we have our data.
	if (request.readyState == 4 && request.status == 200)
	{
		// We can now access the data from request.responseXML.
		var someData = request.responseXML.documentElement.getElementsByTagName('user');
		
		// This data is structured as you'd expect, with children and such.
		// Access a node's data with nodeValue.
		
		var firstUser = someData[0].firstChild.nodeValue;
	}
}

request.open('GET', 'userid.xml' ,true); // Open our request
request.send(); // And send it. When a response comes back, the above function we made will trigger.


Hopefully the comments in the above example give you an idea of what to do. If not, let us know!

This post has been edited by Kruithne: 05 November 2012 - 12:42 PM

Was This Post Helpful? 0
  • +
  • -

#3 shelli06  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 05-November 12

Re: Code Not working for search function

Posted 05 November 2012 - 12:50 PM

View PostKruithne, on 05 November 2012 - 12:41 PM, said:

Hello,

First, you missed a closing code tag, might want to fix that up.

You create a new ActiveXObject, this is only for Internet Explorer, no other browsers. I'm not sure if you intended it to be this way. Below is an example of how to grab data from an XML file from all browsers.

// Check of XMLHttpRequest is supported, if so we use it, else resort to IE ActiveX.
var request = (window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP"));

// Hook an event for when the request is ready.
request.onreadystatechange = function()
{
	// If the readyState is 4 and the status is 200 OK then we have our data.
	if (request.readyState == 4 && request.status == 200)
	{
		// We can now access the data from request.responseXML.
		var someData = request.responseXML.documentElement.getElementsByTagName('user');
		
		// This data is structured as you'd expect, with children and such.
		// Access a node's data with nodeValue.
		
		var firstUser = someData[0].firstChild.nodeValue;
	}
}

request.open('GET', 'userid.xml' ,true); // Open our request
request.send(); // And send it. When a response comes back, the above function we made will trigger.


Hopefully the comments in the above example give you an idea of what to do. If not, let us know!




I will try this out. I am still learning how to use xml in reference with my javascript so the request piece is a little confusing. thank you.

michele
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6037
  • View blog
  • Posts: 23,432
  • Joined: 23-August 08

Re: Code Not working for search function

Posted 05 November 2012 - 12:56 PM

I has a sad that they're teaching you something Microsoft-specific.

Where I run a Mac, I am unable to assist.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1