Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Query XML file and display within HTML

From: bart@------.--- (---- --- --- -----)
To: NULL
Date: 1/2/2005 2:32:00 AM
Jomo (jomo@d...) 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


transparent
Print
Mail
Digg
delicious
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent