or if you really want to use HTML::TreeBuilder something like this may do the trick:

use warnings; use strict; use HTML::TreeBuilder; my $Tree = HTML::TreeBuilder->new (); $Tree->parse ("<HTML><body><p>First para.<br>Second line.</p><p>Second + para</p></body></HTML>"); my $Text; my $empty_element_map = $Tree->_empty_element_map; my(@C) = [$Tree]; # a stack containing lists of children # I is a stack of indexes to current position in corresponding lists i +n @C # In each of these, 0 is the active point my(@I) = (-1); # initial value must be -1 for each list my @Context = ""; # Contains stack of current nodes # scratch: my $this; # current node my $content_r; # child list of $this my $TagName; # Loop over the tree while (@C) { # Post processing # Move to next item in this frame if(!defined($I[0]) or ++$I[0] >= @{$C[0]}) { $this = $Context [0]; if (defined $this and ref $this) {# Close tag my $StartTag = $this->starttag (); $StartTag =~ s/[\r\n]*//gs; if ($StartTag =~ /^<p\b.*?>/g) { $Text .= "</p>"; } } shift @Context; shift @I; shift @C; next; } $this = $C[0][$I[0]]; if (! ref $this) {# Add the text $Text .= $this; } else {# Process this element my $StartTag = $this->starttag (); $StartTag =~ s/[\r\n]*//gs; if ($StartTag =~ /^(<(?:p|br)\b.*?>)/g) { $Text .= "$1" } } # Now queue up content list for the current element... if( ref $this and not ( # ...except for those which not($content_r = $this->{'_content'} and @$content_r) and # ...have empty content lists $this->{'_empty_element'} || $empty_element_map->{$this->{'_tag' +} || ''} # ...and that don't get post-order callbacks ) ) { unshift @Context, $this; unshift @I, -1; unshift @C, $content_r || []; } } print $Text;
<p>First para.<br>Second line.</p><p>Second para</p>

Perl is Huffman encoded by design.

In reply to Re: HTML::TreeBuilder Remove all HTML by GrandFather
in thread HTML::TreeBuilder Remove all HTML by Anonymous Monk

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.