O'Reilly logo

Perl & LWP by Sean M. Burke

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Attaching in Another Tree

So far we've detached elements from one part of a tree and attached them elsewhere in the same tree. But there's nothing stopping you from attaching them in other trees.

For example, consider a case like the above example, where we extract the text in the <td class="story"> ... </td> element, but this time, instead of attaching it elsewhere in the same document tree, we're attaching it at a certain point in a different tree that we're using as a template. The template document looks like this:

<html><head><title>Put the title here</title></head>
<body><!-- printable version -->
<blockquote>
<font size="-1">
<!-- start -->
...put the content here...
<!-- end -->
<hr>Copyright 2002.  Printed from the United Lies Syndicate web site.
</font>
</blockquote>
</body></html>

You'll note that the web designers have helpfully inserted comments to denote where the inserted content should start and end. But when you have HTML::TreeBuilder parse the document with default parse options and dump the tree, you don't see any sign of the comments:

<html> @0
  <head> @0.0
    <title> @0.0.0
      "Put the title here"
  <body> @0.1
    <blockquote> @0.1.0
      <font size="-1"> @0.1.0.0
        "  ...put the content here...  "
        <hr> @0.1.0.0.1
        "Copyright 2002. Printed from the United Lies Syndicate web site. "

Retaining Comments

However, storing comments is controlled by an HTML::TreeBuilder parse option, store_comments( ), which is off by default. If we parse the file like so:

use strict; use HTML::TreeBuilder; my ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required