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
In reply to Re: Walk through HTML::Treebuilder
by Tux
in thread Walk through HTML::Treebuilder
by perlmonkdr
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |