HTML::TreeBuilder

davorg on 2002-07-12T20:25:41

HTML::TreeBuilder rocks.

Twice in the last week, I've wanted to create a table of contents for an HTML document. In both cases they've been documents that I've written, so the structure has been pretty well defined and I started to wonder whether I could automate the process.

I decided that HTML::TreeBuilder would be the right tool and started to take a closer look at the module. It's the first time I've every really used it for anything useful.

The result (after about an hour of hacking) is toc.pl. Currently it's pretty closely tied to the structure of my documents (it relies on the existance of <div> sections called "front" and "body") but it can very easily be used as a basis for other similar scripts.

Share and enjoy :)


I have been impressed too

mir on 2002-07-12T20:45:17

I did not know that module, but I have been really impressed as I was reading about it in Sean's (excellent) "Perl & LWP".
So impressed in fact that I stole a couple of its methods and added them to XML::Twig ;--)

Re:I have been impressed too

ajt on 2002-07-13T07:21:46

TreeBuilder is great, as it's close relative XML::TreeBuilder. However they use both use HTML::Entities as found in HTML::Parser, and can't deal with some valid utf-8 HTML entites because of underlying problems with Perl (or so I'm told). When 5.8 goes final, I for one will upgrade just to get the maximum out of these great modules.

Both make good efforts at dealing with dirty HTML and XML, where faster tools die...

Neither tool is perfect, but both are pretty good, and Sean answers his email