JayBee has asked for the wisdom of the Perl Monks concerning the following question:

I've been trying to use HTML::TagParser to pull reviews from Yelp and modifying formatting slightly to post on our website, though I'm having difficulty parsing through child nodes of the HTML. So I started a basic html file to play with, and still having some difficulty getting the beginning and ending tags to encapsulate properly with inner-text. Here's the basic version of my HTML
<table> <tr id="row1"> <td><div id="r1d1"><b>Test11</b></div></td> <td><div id="r1d2"><i>Test12</i></div></td> <td><div id="r1d3">test13</div></td> <td><b>test14</b></td> </tr> <tr id="row2"> <td><div id="r2d1"><b><em>Test21</em></b></div></td> <td>2</td> <td>3</td> <td>4</td> </tr> </table>
and my code, which pulls things out of place since I can't seem to get my mind to grasp the structuring correctly. Certainly I'm missing something about the "parentNode" after a "childNode" is found... and perhaps "nextSibling" can be useful somewhere.
#!/usr/bin/perl use strict; use CGI ':standard'; use HTML::TagParser; my @Body; my $html=HTML::TagParser->new('table.txt'); my @elem=$html->getElementsByTagName("table"); foreach my $parent(@elem){ my $text=$parent->innerText(); my $tag=$parent->tagName; my $get=$parent->childNodes(); if (@$get){push @Body,digChild(@$get);} push @Body,"<$tag>$text</$tag>"; } #foreach parent print header,start_html('table'); print join("\n",@Body)."\n"; print end_html; sub digChild { my @ar=@_; foreach my $row(@ar){ my $tag=$row->tagName; my $txt=$row->innerText; my $c=$row->childNodes(); if (@$c){push @Body,digChild(@$c);} return "<$tag>$txt</$tag>"; } # foreach row } # sub
Thank you.

Replies are listed 'Best First'.
Re: Digging through HTML Formatting with HTML::TagParser
by Anonymous Monk on Aug 29, 2014 at 22:30 UTC
      I actually don't need it for tables. I was just using this table as an example for a structured html layout. The final product uses nested DIV,META,SPAN,A and other tags.