Balakumar Balakumar - 1 year ago 69
Perl Question

Need suggestion in printing the matched result one by one by using HTML-TreeBuilder-XPath findnodes() method

I am parsing an html content by using HTML-TreeBuilder-XPath
in Perl . i have got the xpath location of the data i need. The issue i am facing is ,There are several matches of the xpath

which is returned by single result ,but i need to print it one by one. Need some suggestion .Thank you.

Answer Source

You can iterate over using

  for my $node (@$paraelements)  { ..... }

A more complete example

  use HTML::TreeBuilder::XPath;
  my $tree= HTML::TreeBuilder::XPath->new;
  $tree->parse_file( "mypage.html");
  my $paraelements= $tree->findnodes( '//p') ;

  for my $node (@$paraelements)  {
      say $node->as_HTML() ;