Query XML file and display within HTML

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

  1. Jomo () wrote:

    > That's great. Thanks.
    > Now what if I want to display the results within a
    > specific section of an existing HTML page?


    Not a problem. You could make a fixed string inside your HTML file (eg
    inside a comment tag). Then read the file and replace that string by
    your XML output result, before showing in to screen.

    Say this is your HTML template, stored as /path/to/your/file.html:

    <html>
    <body>
    <hr>My header<hr>
    <!--g58s5s26d58esskolKjdX-->
    <hr>My footer<hr>
    </body>
    </html>

    Your CGI file could then look like this:

    #!/usr/bin/perl
    print "Content-Type: text/html\n\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)
    {
    $result.=$record->{"URL"}->[0];
    $found=1;
    }
    }
    if ($found!=1)
    {
    print "<html><body>
    No website found
    </body></html>";
    }
    else
    {
    open (R, "/path/to/your/file.html") || die "$!";
    flock(R, 1) || die "Cant get LOCK_SH on file: $!";
    while (<R>)
    {
    $_=~s/<!--g58s5s26d58esskolKjdX-->/$result/i;
    print $_;
    }
    close R;
    }

    --
    Bart
    Bart Van der Donck, Jan 15, 2005
    #1
    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 2, 2005, in forum: XML
    Replies:
    1
    Views:
    3,868
  3. Marek Kurowski
    Replies:
    1
    Views:
    993
    Mark A. Odell
    Aug 18, 2004
  4. Ultrus
    Replies:
    3
    Views:
    378
    Stefan Behnel
    Jul 9, 2007
  5. Cal Who
    Replies:
    12
    Views:
    598
    Cal Who
    Dec 22, 2009
Loading...

Share This Page