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.

In reply to Digging through HTML Formatting with HTML::TagParser by JayBee

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.