Query XML file and display within HTML

B

Bart Van der Donck

Jomo said:
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;
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top