Searching XML Data

You can do a great many things with recordsets, as we've seen in this chapter. In this chapter's final example, I'll take a look at how to search a database for a specific item. In particular, I'll let the user search for a match to a customer's name that the user specifies.

For this example, I'll modify ch08_03.xml by adding a second customer with the name Nancy to make sure we catch all instances of a match, and I'll name this new document ch08_13.xml:

Listing ch08_13.xml
 <?xml version="1.0"?> <CUSTOMER>     <CUSTOMER>         <NAME>Charles</NAME>         <CUSTOMER_ID>58704</CUSTOMER_ID>         <PURCHASE_DATE>10/15/2003</PURCHASE_DATE>         <DEPARTMENT>Meat</DEPARTMENT>         <PRODUCT_NAME>Ham</PRODUCT_NAME>     </CUSTOMER>     <CUSTOMER>         <NAME>Franklin</NAME>         <CUSTOMER_ID>58705</CUSTOMER_ID>         <PURCHASE_DATE>10/15/2003</PURCHASE_DATE>         <DEPARTMENT>Produce</DEPARTMENT>         <PRODUCT_NAME>Tomatoes</PRODUCT_NAME>     </CUSTOMER>     <CUSTOMER>         <NAME>Phoebe</NAME>         <CUSTOMER_ID>58706</CUSTOMER_ID>         <PURCHASE_DATE>10/15/2003</PURCHASE_DATE>         <DEPARTMENT>Meat</DEPARTMENT>         <PRODUCT_NAME>Turkey</PRODUCT_NAME>     </CUSTOMER>     <CUSTOMER>         <NAME>Mark</NAME>         <CUSTOMER_ID>58707</CUSTOMER_ID>         <PURCHASE_DATE>10/15/2003</PURCHASE_DATE>         <DEPARTMENT>Meat</DEPARTMENT>     <PRODUCT_NAME>Beef</PRODUCT_NAME>     </CUSTOMER>     <CUSTOMER>         <NAME>Nancy</NAME>         <CUSTOMER_ID>58708</CUSTOMER_ID>         <PURCHASE_DATE>10/15/2003</PURCHASE_DATE>         <DEPARTMENT>Frozen</DEPARTMENT>         <PRODUCT_NAME>Broccoli</PRODUCT_NAME>     </CUSTOMER>  <CUSTOMER>   <NAME>Nancy</NAME>   <CUSTOMER_ID>58709</CUSTOMER_ID>   <PURCHASE_DATE>10/15/2003</PURCHASE_DATE>   <DEPARTMENT>Produce</DEPARTMENT>   <PRODUCT_NAME>Tomatoes</PRODUCT_NAME>   </CUSTOMER>  </CUSTOMER> 

After setting up an XML data island and connecting it to ch08_13.xml, I'll define a new function named findMatches that will search for matches to the customer name that the user wants to search for. Although ADOR recordset objects do have a method called find to search databases with, you set the search criterion in that method using a Structured Query Language (SQL, the language many database applications use) expression, and Internet Explorer doesn't appear to support such expressions. Instead, I'll set up a JavaScript loop that will find matches to the name that the user is searching for.

The user can enter the name to search for in a text field I'll call text1 . When the user clicks a button, the findMatches function is called. I'll convert the name to search for into lower case (using the JavaScript String object's toLowerCase method), to make the search noncase sensitive, and I'll store it in a variable named searchFor :

 <HTML>      <HEAD>         <TITLE>             Searching XML-Based Databases         </TITLE>         <XML ID="customers" SRC="ch08_13.xml"></XML>         <SCRIPT LANGUAGE="JavaScript">             function findMatches()             {  var searchFor = form1.text1.value.toLowerCase()  .     .     . 

Now I'll loop over all the records in the recordset, storing the name in the current record in a variable named currentName , which I also convert to lower case:

 function findMatches()              {                 var searchFor = form1.text1.value.toLowerCase()  while (!customers.recordset.EOF) {   var currentName = new String(customers.recordset("NAME"))   currentName = currentName.toLowerCase()   .   .   .   customers.recordset.moveNext()   }  } 

I'll use the JavaScript String object's indexOf method to see if the current name matches the name that the user is searching for. The indexOf method returns a value of or greater if a match was found, so I use that method like this:

 function findMatches()              {                 var searchFor = form1.text1.value.toLowerCase()                 while (!customers.recordset.EOF) {                     var currentName = new String(customers.recordset("NAME"))                     currentName = currentName.toLowerCase()  if (currentName.indexOf(searchFor) >= 0) {   .   .   .   }  customers.recordset.moveNext()                 }             }     .     .     . 

All that remains is to display the matching records in a <DIV> element, as we have before in this chapter, and add the button and text field that we'll use to let the user interact with our code:

Listing ch08_14.html
 <HTML>     <HEAD>         <TITLE>             Searching XML-Based Databases         </TITLE>         <XML ID="customers" SRC="ch08_13.xml"></XML>         <SCRIPT LANGUAGE="JavaScript">             function findMatches()             {                 var searchFor = form1.text1.value.toLowerCase()                 while (!customers.recordset.EOF) {                     var currentName = new String(customers.recordset("NAME"))                     currentName = currentName.toLowerCase()                     if (currentName.indexOf(searchFor) >= 0) {  divMessage.innerHTML +=   customers.recordset("NAME") +   " (ID " +   customers.recordset("CUSTOMER_ID") +   ") bought " +   customers.recordset("PRODUCT_NAME") +   " from the " +   customers.recordset("DEPARTMENT") +   " department on " +   customers.recordset("PURCHASE_DATE") +   ".<BR>"  }                     customers.recordset.moveNext()                 }             }         </SCRIPT>     </HEAD>     <BODY>         <CENTER>             <H1>                 Searching XML-Based Databases             </H1>  <FORM ID="form1">   Search for this name: <INPUT TYPE="TEXT" NAME="text1">   <BR>   <BR>   <INPUT TYPE="BUTTON" VALUE="Search for matches"   ONCLICK="findMatches()">   </FORM>  </CENTER>         <DIV ID="divMessage">         </DIV>     </BODY> </HTML> 

You can see the results in Figure 8-9, where we've matched both customers named Nancy.

Figure 8-9. Searching for matches in XML-based databases.

graphics/08fig09.gif

In this example, I've used XML data islands, but of course you can also use the XML DSO applet to load the file ch08_13.xml. I'll do that here to demonstrate another aspect of the XML DSO appletup until now, I've given the applet a zero width and height, but in fact, this applet does display the status of its operations if you give it some space in the Web page. I'll do that like this:

Listing ch08_15.html
 <HTML>     <HEAD>         <TITLE>             Searching XML-Based Databases         </TITLE>         <SCRIPT LANGUAGE="JavaScript">             function findMatches()             {                 var searchFor = form1.text1.value.toLowerCase()                 while (!customers.recordset.EOF) {                     var currentName = new String(customers.recordset("NAME"))                     currentName = currentName.toLowerCase()                     if (currentName.indexOf(searchFor) >= 0) {                         divMessage.innerHTML +=                         customers.recordset("NAME") +                         " (ID " +                         customers.recordset("CUSTOMER_ID") +                         ") bought " +                         customers.recordset("PRODUCT_NAME") +                         " from the " +                         customers.recordset("DEPARTMENT") +                         " department on " +                         customers.recordset("PURCHASE_DATE") +                         ".<BR>"                     }                     customers.recordset.moveNext()                 }             }         </SCRIPT>     </HEAD>     <BODY>         <CENTER>             <H1>                 Searching XML-Based Databases             </H1>  <APPLET CODE="com.ms.xml.dso.XMLDSO.class"   ID="customers"   WIDTH="400" HEIGHT="50"   MAYSCRIPT="true">   <PARAM NAME="URL" VALUE="ch08_13.xml">   </APPLET>  <FORM ID="form1">                 Search for this name: <INPUT TYPE="TEXT" NAME="text1">                 <BR>                 <BR>                 <INPUT TYPE="BUTTON" VALUE="Search for matches"                     ONCLICK="findMatches()">             </FORM>         </CENTER>         <DIV ID="divMessage">         </DIV>     </BODY> </HTML> 

You can see the results in Figure 8-10, where the XML DSO is displaying the message Successfully loaded XML from "file:/C:/xml/chap08/ch08_13.xml" . The background of this applet is green because there was no error. If there was an error, however, the background would be red and the applet would display error messages.

Figure 8-10. Searching for matches in XML-based databases using the XML DSO.

graphics/08fig10.gif



Real World XML
Real World XML (2nd Edition)
ISBN: 0735712867
EAN: 2147483647
Year: 2005
Pages: 440
Authors: Steve Holzner

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net