SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Feb 2007
    Location
    Sacramento
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    XML as small database and AJAX

    Hello all. I am new here so please forgive me if I sound like an idiot. I have been tasked with creating a simple store search for a clients website. Normally I would put the info in a database and use a language to search the fields but this is an unusual circumstance where the server is really stripped down and the client will not move the site. With that in mind I am thinking I can use AJAX to search for the cities in an XML list. A Sample of the XML might look like this.

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <locations>
    <location>
    <storeName>RALPHS</storeName>
    <address>5727 KANAN RD</address>
    <city>AGOURA</city>
    <state>CA</state>
    <zip>91301</zip>
    <phone>818-889-5429</phone>
    </location>

    <location>
    <storeName>RALPHS</storeName>
    <address>26901 ALISO CREEK RD</address>
    <city>ALISO VIEJO</city>
    <state>CA</state>
    <zip>92656</zip>
    <phone>949-362-3727</phone>
    </location>
    </locations>

    Having never used AJAX or PHP for something like this can someone help me to figure out how to do this. I have been searching for several hours and really do not know the terminology. I found something called xpath but have no idea how it works exactly. Sounds like it is at least a component in the process???

    Any thoughts? Is this even possible? am I wasting my time and should just do a static HTML page for each city? Any help you can give me would be greatly appreciated. Even a link or two. Thanks!

  2. #2
    SitePoint Addict
    Join Date
    Apr 2001
    Location
    Devon, UK
    Posts
    333
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    AJAX can be used, but it will load the whole XML document into JavaScript - that's only practical if the file size isn't too big.

    You can then examine nodes in that XML using yourXML.getElementById and yourXML.getElementByTagName just as you would when navigating the DOM.

    It might be an idea to parse the whole file and read it into an array of objects. Sorting and searching should then be much faster.

    Final note: JS/AJAX can only examine a file from the same server that sent the page.

  3. #3
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Good question, it's on my list of stuff to find out too.

    You have the right idea with xpath, I know you can do it in IE but a cross-browser solution I don't think exists. I created a new thread asking that specifically...
    http://www.sitepoint.com/forums/showthread.php?t=460735

    PS, in IE you can do this:
    Code:
    var ajax = new ActiveXObject("msxml.xmlhttp");
    // code to execute ajax request and return xml goes here
    
    var dom = ajax.responseXML;
    // use xpath to get the location record where city=ALISO VIEJO
    var record = dom.selectSingleNode("records/record[city='ALISO VIEJO']");
    // get the phone number element for record with zip=91301
    var record = dom.selectSingleNode("records/record[zip='91301']/phone");
    // use xpath to get ALL RECORDS with state = ca
    var records = dom.selectNodes("records/record[state='ca']");
    hope that helps


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •