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

Hi! Guys

How are you in this new year!!! I hope that so fine...

Well, I'm now working with HTML::Treebuilder, on this, I will want to add to each tag the address of this like a new attribute, but couldn't found the way to walk through the complete tree...

See the example of what i'm talking:

<html addr="0"> <head addr="0.0"> <title addr="0.0.0">Seekers of Perl Wisdom</title> <meta name="Content-type" content="text/html" addr="0.0.1" /> </head> <body addr="0.1"> <h1 addr="0.1.0">Hello World!!!</h1> </body> </html>

Do you know some way to do that, HTML::Element->right() appear not to be the way, and google don't suggest nothing useful.

Thank You and happy new year

Replies are listed 'Best First'.
Re: Walk through HTML::Treebuilder
by Tux (Canon) on Jan 06, 2008 at 09:05 UTC

    I'd have a look at HTML::Element's dump method, as it already prints those addresses as you propose at the end of each tag. Try $tree->dump to see what I mean

    Or you can roll your own like this:

    #!/pro/bin/perl use strict; use warnings; use HTML::TreeBuilder; my $tree = HTML::TreeBuilder->new; my @l; $tree->parse_content (<DATA>); foreach my $e ($tree->look_down (_tag => qr{.})) { push @l, -1; splice @l, 1 + $e->depth; $l[-1]++; $e->attr ("addr", join ".", @l); } print $tree->as_HTML (undef, " ", {}); __END__ <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> <html> <head> <title>Testing SNMP addr</title> <meta name="Content-type" content="text/html;charset=utf-8"> <meta name="Generator" content="elvis 2.2"> <meta name="Author" content="H.Merijn Brand"> </head> <body> <h1>Hello world</h1> <p>Some content</p> </body> </html>

    That would give me:

    <html addr=0> <head addr="0.0"> <title addr="0.0.0">Testing SNMP addr</title> <meta addr="0.0.1" content="text/html;charset=utf-8" name="Content +-type"> <meta addr="0.0.2" content="elvis 2.2" name="Generator"> <meta addr="0.0.3" content="H.Merijn Brand" name="Author"> </head> <body addr="0.1"> <h1 addr="0.1.0">Hello world</h1> <p addr="0.1.1">Some content</p> </body> </html>

    Edit: removed conditionals inside for loop


    Enjoy, Have FUN! H.Merijn

      I was really stuck trying to see how to walk through the tree and was not coming to a valid solution.

      Thank you so much, worked perfectly.

      Greetings and thanks again

Re: Walk through HTML::Treebuilder
by Anonymous Monk on Jan 06, 2008 at 07:35 UTC
      add_address_attr($tree); sub add_address_attr { my $self = shift; for (@{$self->{'_content'}}) { if (ref $_) { # element $_->attr(addr => $_->address ); add_address_attr ($_); # recurse } } }

        I really don't occurred to me walk through the hash, thanks you very good and beautiful solution.

        I was looking for some method that walk to it, but this is a great idea

        Thank you again

        combined with the solution I proposed, that would be even more short and simple:

        $tree->parse_content (<DATA>); $_->attr ("addr", $_->address) for $tree->look_down (_tag => qr{.});

        I never knew about the address property. Thanks, very useful


        Enjoy, Have FUN! H.Merijn