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.<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>
Thank you.#!/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
In reply to Digging through HTML Formatting with HTML::TagParser by JayBee
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |