Gossamer Forum
Home : General : Perl Programming :

about xml2html.pl

Quote Reply
about xml2html.pl
i had a problem about the under script ...

#!/usr/bin/perl
################################################
# CONFIG PART, CHANGE TO FIT YOUR ENV.
################################################
#$D = 1; # Uncomment for debug output
#
$cgi=1; # Comment out if script is not use for cgi-bin
#
# Path to your template file
$template="/home/user/temp/xmltemplate.html";
#
# URL to the XML file you want to get ( when using this path it can work ) <-first path
$xmlURL="
http://www.user.net/test.xml";
#
# URL to the XML file you want to get ( if using this path it can't work ) <-second path
$xmlURL="
http://www.user.org/try.php3?user1999@mail.net+xml";
#
# HTML output file
$out = "/home/user/temp/b.html";
#
# Update interval in seconds
$int = 1800;
#
# Set $level to 1 if your xml looks like this:
# <item>
# <item2>
# <something>blabla</something>
# </item2>
# <item2>
# <something>blabla</something>
# </item2>
# </item>
#
#$item = 1;
#
# Maximum numbers of items/articles
$maxitems = 5;
################################################
# CONFIG END
################################################

use LWP::Simple;

open(TMPL,"$template") || die "Could not read $template: $!\n";
@tmpl = <TMPL>;
close(TMPL);

# Find template keys
foreach $line ( @tmpl ) {
chop($line);
if ( !$head && !$foot ) {
($line,$trash) = split(/\s*#/,$line);
}
if ( $line eq STARTCONFIG ) {
$config = 1;
undef $tmplpart;
undef $foot;
undef $head;
}
if ( $line eq HTMLFOOT ) {
$foot = 1;
undef $config;
undef $tmplpart;
undef $head;
}
if ( $line eq HTMLHEAD ) {
$head = 1;
undef $config;
undef $tmplpart;
undef $foot;
}
if ( $line eq STARTTEMPLATE ) {
$tmplpart = 1;
undef $config;
undef $head;
undef $foot;
}
if ( $config ) {
($key,$val) = split(/=/,$line);
$TMPL{$val} = $val if ( $val );
}
if ( $tmplpart && $line !~ /START/ ) {
$tmplhtml .= $line;
}
if ( $foot && $line !~ /FOOT/ ) {
$htmlfoot .= $line."\n";
}
if ( $head && $line !~ /HEAD/ ) {
$htmlhead .= $line."\n";
}
}

print "HTML template -> $tmplhtml\n" if ( $D );
if ( $D ) {
print "Template keys found:\n";
foreach $key ( keys(%TMPL) ) {
print "$key -> $TMPL{$key}\n";
}
}
$now = time;
#unlink($out);
if ( -f $out ) {
($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks) = stat($out);
$diff = $now - $mtime;
}

print "Content-type: text/html\n\n" if ( $cgi );
if ( $diff > $int | !-f $out) {
$xml=get($xmlURL);
$xml =~ s/<\?.*?>//g;
#$xml =~ s/\s+/ /g;
print "Got this from $xmlURL -->\n$xml" if ( $D );
#($itemsep) = $xml =~ /<(.*?)>/;
if ( !$level ) {
($itemsep,@items) = &finditem($xml);
$xml =~ s/<$itemsep>//g;
$xml =~ s#</$itemsep>##g;
}
($itemsep,@items) = &finditem($xml);
print "\nItem seperator -> $itemsep\n" if ( $D );
$ant = @items;
print "\nNumber of items -> $ant\n" if ( $D );
open(OUT,">$out") || die "Failed to write to $out: $!\n";
print OUT "$htmlhead\n";
foreach $item ( @items ) {
$numitem++;
if ( $numitem > $maxitems )
{
print "MAX!!! numitem -> $numitem\tmaxitem -> $maxitem\n" if ( $D );
last;
}
$item =~ s/\n/ /g;
$item =~ s/\015/ /g;
$html = $tmplhtml;
print "\n----------------------------------\n" if ( $D );
print "$itemsep\n" if ( $D );
print "----------------------------------\n" if ( $D );
print "\nItem $ino ->\n$item\n" if ( $D );
#(@ind) = $item =~ m#<(.*?)>(.*?)</$1>#g;
foreach $key ( keys(%TMPL) ) {
($cont) = $item =~ m#<$key>(.*?)</$key>#g;
$IND{$key} = $cont;
}
#foreach $in (@ind) {
# print "in -> $in\n" if ( $D );
#}
#%IND = @ind;
foreach $key ( keys(%IND) ) {
print "$key -> $IND{$key}\n" if ( $D );
print "HTML line: $html\n" if ( $D );
print "Substitue: $TMPL{$key} with $IND{$key}\n" if ( $D );
$html =~ s/$TMPL{$key}/$IND{$key}/;
}
undef(%IND);
print "HTML->\n" if ( $D );
print OUT "$html\n";
print "$html\n" if ( $D );
print "END HTML\n" if ( $D );
}
print OUT "$htmlfoot\n";
}

sub finditem {
my $xmls = shift;
my $isep;
my @items;

($isep) = $xmls =~ /<(.*?)>/;
print "\n (sub) Itemsep -> $isep\n" if ( $D );
(@items) = $xmls =~ m#<$isep>(.*?)</$isep>#gs;

return $isep,@items;
}

open (OUT,"$out") || die "read error: $!\n";
while(<OUT>) {
print;
}
close(OUT);


----------------------------------------------------------------------

try it many times, i guess is the symbol code ?@+ make it wrong!

does anyone can teach me how to fix it running by the second path? thank much.
Quote Reply
Re: [PDK1999] about xml2html.pl In reply to
\@
Quote Reply
Re: [Paul] about xml2html.pl In reply to
/ in where?

and the ? and +

how to / it
Quote Reply
Re: [PDK1999] about xml2html.pl In reply to
sorry ...

\ in where

and the ? and +

how to \ it
Quote Reply
Re: [PDK1999] about xml2html.pl In reply to
I showed you what to change:

@

to

\@
Quote Reply
Re: [Paul] about xml2html.pl In reply to
OH! Paul , thank you very much. Smile
Quote Reply
Re: [PDK1999] about xml2html.pl In reply to
Why this script can't print " $maxitems = 5 "

it only print the first item/articles on the template.html ?

someone can help me how to do it right. Thank's