in reply to HTML::Tree: get value of an element

Try
#!perl use strict; use HTML::TreeBuilder; my $body = HTML::TreeBuilder->new_from_file(\*DATA); my @subsub = $body->look_down (_tag => "span"); foreach my $sss (@subsub) { my $c = $sss->attr("class"); if ($c =~ /name color/){ # ignore sub elements for ( $sss->descendants() ){ $_->detach(); }; my $name = $sss->as_trimmed_text(); print $name."\n"; } } __DATA__ <span class="name color1">ImportantText1</span> <span class="name color2"><span class="level">1000</span>ImportantText +2</span>
poj

Replies are listed 'Best First'.
Re^2: HTML::Tree: get value of an element
by Ratazong (Monsignor) on Feb 20, 2015 at 07:51 UTC

    Thanks for your suggestion! I tested it, and it works fine. :-)

    Nevertheless, I solved the problem another way (as I don't like the idea of destroying my tree): I now additionally create the texts for the sub-elements, and then remove them from the top-element.

    This solution also doesn't make me happy (it is somehow waste) - however I don't have any problems with runtime or memory-usage now, so it doesn't hurt. And the additional amount of electrons moving around heats up the air in the room - which is not to bad in winter ;-)

    Rata