Continually Improving.... let us know how support@devdiamond.net Sign in | Sign up
home articles news blog forum  

 
 


 
Skip Navigation LinksHome > Article > How to Search XML file
How to Search XML file
Abstract :
See how you can search an XML File without alot of code, so you can use XML file as database, its Easy!

Views : 13773
Published : Wednesday, April 03, 2002
By
HyperLink

Avarage Rating :
Page Page 1 of 1

On the How to use XML file as database article I have mentioned how to connect to a XML file and how to retrieve data from.

I will include all codes needed here but without much descriptions about the connection here, I recommend you first read How to use XML file as database.

On this article I will search our ASP RSS file (RSS.xml), for more information about ASP RSS go to www.asprss.com
For short description, asp RSS files is a standard way of publishing data to asp directories, imagine you write articles every week or even every month, on the old way you need to submit every article you write to every directory meaning a lot of waist of time. With asp RSS files you just make your RSS file, and submitted one time to the directories.


So let's get started.
Our RSS files is at http://www.minwar.com/RSS.xml, it's what we need to make a connection to

Option Explicit

Dim xmlVisitor
Dim blnFileExist
Dim intMaxRow, intRow, intCol, intMaxCol
Dim sSearchkey
Dim strAuthor, strDesc, strDescR
Dim sSplitResults

sSearchkey = request.querystring("q")

Set xmlVisitor = Server.CreateObject("Microsoft.XMLDOM")
sXMLFile = xmlVisitor.load(server.MapPath("RSS.xml"))

this code connects to the file and load it the Microsoft.XMLDOM object is usually built-in on you Microsoft OS. If it's not let me know.

if sXMLFile = False Then
    response.write "No record Found"
Else
    With xmlVisitor.documentElement
        intMaxRow = .childNodes.length - 1
        intMaxCol = .childNodes(2).childNodes.length - 1

        Response.write "<table border='1' width='100%' cellspacing='0'>"
        Response.write "<tr>"
        Response.write "</tr>"
        Dim a_strURLs(200)
        Dim a_strResults
        Dim i
        for intRow = 2 to intMaxRow
            a_strURLs(intRow) = .childNodes(intRow).childnodes(0).text & "MYMARK" & .childNodes(intRow).childnodes(1).text & "MYMARK" & .childNodes(intRow).childnodes(2).text
        Next
        a_strResults = Filter(a_strURLs, sSearchKey, -1, 1)
        for i = 0 to Ubound(a_strResults)
            sSplitResults = Split(a_strResults(i), "MYMARK", -1, 1)
            Response.write "<tr>"
            Response.write "<td>"
            Response.write "<a href='" & sSplitResults(2) & "'>" & sSplitResults(0) & "</a><br>"
            Response.write "</TD>"
            Response.write "</Tr>"
            Response.write "<tr>"
            Response.write "<td>"
            Response.write sSplitResults(1) & "<br>"
            Response.write "</TD>"
            Response.write "</tr>"
        Next
    End With
End IF

Here comes the good stuff, the this loop puts all the records on the xml file into one array that we will split later, to display

title
description
link
dc:date
pa:category
FAQ :: What is a WebClass? Read this brief definition of what is a WebClass http://Minwar.com/learn/faq.asp#WebClass 2002-03-17 Articles
FAQ :: What is the Session Object? Read this brief definition of what is the Session Object http://Minwar.com/learn/faq.asp#Session 2002-03-17 Articles
FAQ :: What is a Variable? Read this brief definition of what is a Variable http://Minwar.com/learn/faq.asp#Variable 2002-03-17 Articles

This table is what we will search on this article

I wanted to search the title and the description but I wanted the link too, so I putted all these fields on one array and searched using the Filter() function to see which of these contains the search string and then I printed out the results (title, description and link)

The reason why I putted the three fields into one array is that

  1. I wanted to search them all
  2. cause if I search one field I would not be able to display the others at the end, cause simply XML doesn't have a ID field

Set xmlVisitor = Nothing

clear objects

the only problem with this code that it does search one keyword only, if you have ideas to improve this code, contact me at yasir@minwar.com

You can download all the code



About Author

        Yasir Send Feedback
        Yasir is a .NET expert, with over 5 years experience in Microsoft Technologies, 8 years overall programming experience, he is the owner, founder & primary contributor of Minwar.com, and he also works as IT Director in the hospitality industry.