Query XML file and display within HTML

Discussion in 'XML' started by Bart Van der Donck, Jan 2, 2005.

  1. Jomo () wrote:

    > How do I generate a request from an HTML search form
    > (or button/hyperlink) to search/query an XML data file,
    > using CGI scripts, and have the results displayed within
    > an HTML page?


    Here is a (unix) starting point to play around with. It uses Perl's
    XML::Simple (http://search.cpan.org/dist/XML-Simple/).

    Say 3 files in your same directory:
    (1) start.html
    (2) data.xml
    (3) script.pl

    -------------------------------------------
    (1) start.html
    -------------------------------------------
    <html>
    <body>
    <form method=get action=script.pl>
    What is the website of
    <input type=text name=artist>
    <input type=submit value=Search>
    </form>
    </body>
    </html>

    -------------------------------------------
    (2) data.xml
    -------------------------------------------
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <ARTISTWEBSITES>
    <RECORD>
    <ARTIST>Bob Dylan</ARTIST>
    <URL>http://www.bob-dylan.com</URL>
    </RECORD>
    <RECORD>
    <ARTIST>Bonnie Tyler</ARTIST>
    <URL>http://www.bonnie-tyler.com</URL>
    </RECORD>
    <RECORD>
    <ARTIST>Dolly Parton</ARTIST>
    <URL>http://www.dolly-parton.com</URL>
    </RECORD>
    </ARTISTWEBSITES>

    -------------------------------------------
    (3) script.pl
    -------------------------------------------
    #!/usr/bin/perl
    print "Content-Type: text/html\n\n<html>\n<body>\n";
    use CGI::Carp qw(fatalsToBrowser);
    use XML::Simple;
    my $found=0;
    my $q=$ENV{"QUERY_STRING"};
    $q=~s/^artist=//;
    $q=~tr/+/ /;
    $q=~s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
    my $xmlfile = XMLin("data.xml", forcearray=>1);
    for my $record (@{$xmlfile->{RECORD}})
    {
    if (uc $record->{"ARTIST"}->[0] eq uc $q)
    {
    print $record->{"URL"}->[0]; $found=1;
    }
    }
    print "No website found" if ($found!=1);
    print "\n</body>\n</html>";

    XML::Simple is easy and of good quality, but it has a limitation
    though for your xml data regarding so-called 'mixed content'
    (<para>This is <em>mixed</em> content.</para>).

    You find more info at see http://search.cpan.org/dist/XML-Simple/. It
    also says that very large XML files should not be approached by
    XML::Simple due to memory issues.

    HTH
    --
    Bart

    "The most exciting phrase to hear in science is not 'Eureka!' but
    'That's funny...'" - Isaac Asimov
    Bart Van der Donck, Jan 2, 2005
    #1
    1. Advertising

  2. Bart Van der Donck

    Guest

    That's great. Thanks.

    Now what if I want to display the results within a specific section of
    an
    existing HTML page?

    Jomo
    , Jan 7, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    487
    Martin Honnen
    Jan 1, 2005
  2. Bart Van der Donck

    Query XML file and display within HTML

    Bart Van der Donck, Jan 15, 2005, in forum: XML
    Replies:
    0
    Views:
    363
    Bart Van der Donck
    Jan 15, 2005
  3. Marek Kurowski
    Replies:
    1
    Views:
    991
    Mark A. Odell
    Aug 18, 2004
  4. Ultrus
    Replies:
    3
    Views:
    376
    Stefan Behnel
    Jul 9, 2007
  5. Cal Who
    Replies:
    12
    Views:
    596
    Cal Who
    Dec 22, 2009
Loading...

Share This Page