View Full Version : XML output to HTML table
vietboy505
03-06-2006, 04:40 PM
I want my PErl to output the content of XML to HTML table like this:
<table border="1">
<tr>
<th>Type</th>
<th>Contact</th>
</tr>
<tr>
<td>Work</td>
<td><a href="../search.asp?ID=123456">John Doe</a></td>
</tr>
<tr>
<td></td>
<td><a href="../search.asp?ID=05789">Jack Doe</a></td>
</tr>
<tr>
<td>Relative</td>
<td><a href="../search.asp?ID=654321">Jane Doe</a></td>
</tr>
</table>
address.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Comment -->
<Address>
<Work>
<JohnDoe>
<name>John Doe</name>
<email>john@doe.com</email>
<phone>555-555-5555</phone>
<pager>6666666666</pager>
<id>123456</id>
</JohnDoe>
<JackDoe>
<name>Jack Doe</name>
<email>jack@doe.com</email>
<phone>666-666-6666</phone>
<pager>7777777777</pager>
<id>05789</id>
</JackDoe>
</Work>
<Relative>
<JaneDoe>
<name>Jane Doe</name>
<email>jane@doe.com</email>
<phone>444-444-4444</phone>
<pager>8888888888</pager>
<id>654321</id>
</JaneDoe>
</Relative>
</Address>
address2.pl
#!/usr/local/bin/perl
print "Content-Type: text/html\n\n";
use XML::Simple;
use Data::Dumper;
my $ref = XMLin('address.xml');
print('<table border="1">\n
<tr>\n
<th>Type</th>\n
<th>Contact</th>\n
</tr>\n');
foreach my $type (qw(Work Relative)) {
foreach my $person (keys %{$ref->{$type}}) {
my $id=$ref->{$type}->{$person}->{id};
my $name=$ref->{$type}->{$person}->{name};
print("<a href='../search.asp?ID=$id'>$name</a>");
}
}
print('</table>');
I just need how to fix my foreach loop. Any help?
FishMonger
03-06-2006, 05:35 PM
#!/usr/local/bin/perl
use strict;
use warnings;
use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser warningsToBrowser);
use XML::Simple;
use Data::Dumper;
print header();
warningsToBrowser(1);
my $ref = XMLin('address.xml');
print <<TABLE;
<table border="1">
<tr>
<th>Type<th />
<th>Contact<th />
<tr />
TABLE
foreach my $type (qw(Work Relative)) {
my $count = 0;
foreach my $person (keys %{$ref->{$type}}) {
my $id=$ref->{$type}->{$person}->{id};
my $name=$ref->{$type}->{$person}->{name};
$count == 0 ? print "<tr>\n<td>$type<td />\n" : print "<tr>\n<td><td />\n";
print("<td><a href='../search.asp?ID=$id'>$name</a><td />\n<tr />\n\n");
$count++;
}
}print '<table />';
vietboy505
03-06-2006, 05:48 PM
I setup my IIS to read PERL file. Do you know by any chance why my IIS is slow loading up .pl file when I called http://localhost/web/address2.pl with the same code above?
"CGI Timeout
The specified CGI application exceeded the allowed time for processing. The server has deleted the process."
FishMonger
03-06-2006, 05:52 PM
I haven't used IIS for over 5 years, so I can't give any help in that area. Apache is much better.
vietboy505
03-06-2006, 07:08 PM
It work now!
I just need to change c:\perl\perl.exe %s %s in IIS info.
and Use Limit to GET, POST
I try to do a print before the table to include a config.php file and it doesn't work. Why is that, but it does print out
<?php include('config.php') ?>
when I view the source.
print("<?php include(config.php') ?>\n\n");
FishMonger
03-06-2006, 07:30 PM
You might what to ask that question in the php topic area. Personally, I hate using php and only use it when the boss says it's an absolute requirement.
vietboy505
03-06-2006, 08:03 PM
I made some changed in the XML file..
<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Comment -->
<Address>
<XCOM>
<Work>
<JohnDoe>
<name>John Doe</name>
<email>john@doe.com</email>
<phone>555-555-5555</phone>
<pager>6666666666</pager>
<id>123456</id>
</JohnDoe>
<JackDoe>
<name>Jack Doe</name>
<email>jack@doe.com</email>
<phone>666-666-6666</phone>
<pager>7777777777</pager>
<id>05789</id>
</JackDoe>
</Work>
</XCOM>
<YCOM>
<Relative>
<JaneDoe>
<name>Jane Doe</name>
<email>jane@doe.com</email>
<phone>444-444-4444</phone>
<pager>8888888888</pager>
<id>654321</id>
</JaneDoe>
</Relative>
</YCOM>
</Address>
I still want the same output, so i try to put in a different foreach loop, how would that works?
##sampel of html I want to output at the end
#!/usr/local/bin/perl
use strict;
use warnings;
use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser warningsToBrowser);
use XML::Simple;
print header();
warningsToBrowser(1);
my $ref = XMLin('address.xml');
print <<TABLE;
<table border="1">
<tr>
<th>Type<th />
<th>Contact<th />
<tr />
TABLE
foreach my $cat (qw(XCOM YCOM)) {
foreach my $type (qw(Work Relative)) {
my $count = 0;
foreach my $person (keys %{$ref->{$cat}->{$type}}) {
my $id=$ref->{$cat}->{$type}->{$person}->{id};
my $name=$ref->{$cat}->{$type}->{$person}->{name};
$count == 0 ? print "<tr>\n<td>$type<td />\n" : print "<tr>\n<td><td />\n";
print("<td><a href='../search.asp?ID=$id'>$name</a><td />\n<tr />\n\n");
$count++;
}
}
}
print '<table />';
##
##<table border="1">
##<tr>
##<th>Type</th>
##<th>Contact</th>
##</tr>
##<tr>
##<td>XCOM</td>
##<td>Work</td>
##<td><a href="../search.asp?ID=123456">John Doe</a></td>
##</tr>
##<tr>
##<td></td>
##<td><a href="../search.asp?ID=05789">Jack Doe</a></td>
##</tr>
##<tr>
##<td>YCOM</td>
##<td>Relative</td>
##<td><a href="../search.asp?ID=654321">Jane Doe</a></td>
##</tr>
##</table>
Where should I put my counter?
FishMonger
03-06-2006, 09:20 PM
foreach my $cat (qw(XCOM YCOM)) {
print "<tr>\n<td>$cat<td />\n";
foreach my $type (qw(Work Relative)) {
my $count = 0;
foreach my $person (keys %{$ref->{$cat}->{$type}}) {
my $id = $ref->{$cat}->{$type}->{$person}->{id};
my $name = $ref->{$cat}->{$type}->{$person}->{name};
$count == 0 ? print "<td>$type<td />\n" : print "<td><td /><td><td />\n";
print("<td><a href='../search.asp?ID=$id'>$name</a><td />\n<tr />\n\n");
$count++;
}
}
}
vietboy505
03-06-2006, 09:40 PM
I just fixed it, and came back to edit the post, but since you reply to it already.
Thanks for the help.
vietboy505
03-08-2006, 07:26 PM
foo2.xml
<config>
<sahara>
<name>sahara</name>
<id>12345</id>
<address>10.0.0.101</address>
<address>10.0.1.101</address>
</sahara>
<gobi>
<name>gobi</name>
<id>123456</id>
<address>10.0.0.102</address>
</gobi>
<kalahari>
<name>kalahari</name>
<id>123457</id>
<address>10.0.0.103</address>
<address>10.0.1.103</address>
</kalahari>
</config>
foo2.pl
#!/usr/local/bin/perl
use strict;
use warnings;
use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser warningsToBrowser);
use XML::Simple;
print header();
warningsToBrowser(1);
my $ref = XMLin('foo2.xml');
print <<TABLE;
<table border="0">
<tr>
<th>Server<th />
<th>IP<th />
<tr />
TABLE
foreach my $type (qw(sahara gobi kalahari)) {
my $count = 0;
my $counter =0;
my $id=$ref->{$type}->{id};
my $name=$ref->{$type}->{name};
$count == 0 ? print "<tr>\n<td><a href='../search.asp?ID=$id' target='_blank'>$name</a><td />\n" : print "<tr>\n<td><td />\n";
foreach (@{$ref->{$type}->{address}}) {
$counter == 0 ? print "\n<td> $_ <td />\n" : print "\n<tr><td><td /><td>$_<td />\n";
$counter++;
}
$count++;
}
print '<table />';
I want it to output like this:
<table border="0">
<tr>
<th>Server<th />
<th>IP<th />
<tr />
<tr>
<td>sahara</td>
<td>10.0.0.101</td>
</tr>
<td></td>
<td>10.0.1.101</td>
</tr>
<tr>
<td>gobi</td>
<td>10.0.0.102</td>
</tr>
<tr>
<td>kalahari</td>
<td>10.0.0.103</td>
</tr>
<td></td>
<td>10.0.1.103</td>
</tr>
But I got the following errors, about hashing.. Why is that?
Content-Type: text/html; charset=ISO-8859-1
<table border="0">
<tr>
<th>Server<th />
<th>IP<th />
<tr />
<tr>
<td><a href='../search.asp?ID=12345' target='_blank'>sahara</a><td />
<td> 10.0.0.101 <td />
<tr><td><td /><td>10.0.1.101<td />
<tr>
<td><a href='../search.asp?ID=123456' target='_blank'>gobi</a><td />
<tr>
<td><a href='../search.asp?ID=123457' target='_blank'>kalahari</a><td />
<td> 10.0.0.103 <td />
<tr><td><td /><td>10.0.1.103<td />
That is without restrict..
with restrict IS:
Content-Type: text/html; charset=ISO-8859-1
<table border="0">
<tr>
<th>Server<th />
<th>IP<th />
<tr />
<tr>
<td><a href='../search.asp?ID=12345' target='_blank'>sahara</a><td />
<td> 10.0.0.101 <td />
<tr><td><td /><td>10.0.1.101<td />
<tr>
<td><a href='../search.asp?ID=123456' target='_blank'>gobi</a><td />
<h1>Software error:</h1>
<pre>Can't use string ("10.0.0.102") as an ARRAY ref while "stri
t refs" in use at foo2.pl line 32.
</pre>
<p>
For help, please send mail to this site's webmaster, giving this error message
and the time and date of the error.
</p>
[Wed Mar 8 15:55:01 2006] foo2.pl: Can't use string ("10.0.0.102") as an ARRAY
ref while "strict refs" in use at foo2.pl line 32.
How can I fix that with "use strict", and also.. It doesn't print out if there is only one element in the XML file.
FishMonger
03-09-2006, 12:06 AM
This is a perfect example where using Data::Dumper would uncover the problem. The second time through the loop, where you're processing gobi, address is a scalar not an array. Your print statement is de referencing it as an array reference, so Perl is complaining when it see the scalar. You need to test to see what data type you're tyring to print.
if (ref $ref->{$type}->{address} eq 'ARRAY') {
print "@{$ref->{$type}->{address}}"; #print all array
} else {
print $ref->{$type}->{address};
}
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.