[ABE.pm] Using TreeBuilder
Faber Fedor
faber at linuxnj.com
Tue Nov 15 13:55:57 PST 2005
There's something (well, many things, I'm sure!) I'm not getting about
TreeBuilder. Here's an example of what I'm trying to do:
I've got a table that looks like this:
<HTML>
<BODY>
<table>
<tr>
<td>Kiss</td> <td>Alive!</td> <td>Dressed To Kill</td>
</tr>
<tr>
<td>Kate Bush</td> <td>The Kick Inside</td> <td>Hounds of Love</td> <td>Ariel</td>
</tr>
<tr>
<td>The Who</td> <td>The Kids are Alright</td> </td>Who's Next</td>
</tr>
</table>
</BODY>
</HTML>
I want to loop through the table and if the first column is equal to
"Kate Bush", put the rest of the row into
variables/arrays/hashes/whatever.
Not only can I not get past the first row, the code to print the
contents of the data cells is ugly, IMO:
#!/usr/bin/perl -w
use strict;
use HTML::TreeBuilder;
# Let's walk through the forms....
my $tree = HTML::TreeBuilder->new;
$tree->parse_file("simple.html");
# we found a table row
my $node = $tree->look_down("_tag", "tr") ;
#this prints out the first row
foreach my $item ($node->content_list()) {
foreach my $jtem ($item->content_list()){
print "jtem is $jtem\n";
}
}
# find the next row
print "Looking for the next row\n";
# I assumed this would increment to the next row, but it doesn't. :-(
# it prints the first row again.
$node = $tree->look_down("_tag", "tr");
foreach my $item ($node->content_list()) {
foreach my $jtem ($item->content_list()){
print "jtem is $jtem\n";
}
}
#the first row, again!
$node = $tree->look_down("_tag", "td");
print @{$node->content()}[0]."\n";
if (@{$node->content()}[0] eq "Kate Bush") {
print @{$node->content()}[0]."\n";
print @{$node->content()}[1]."\n";
print @{$node->content()}[2]."\n";
}
$tree->delete();
exit(0);
--
Regards,
Faber Fedor
President
Linux New Jersey, Inc.
908-320-0357
800-706-0701
http://www.linuxnj.com
More information about the ABE-pm
mailing list