home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / share / doc / libxml-grove-perl / examples / test-sub.pl < prev    next >
Encoding:
Text File  |  1999-10-23  |  660 b   |  34 lines

  1. use XML::Parser;
  2. use XML::Parser::Grove;
  3. use XML::Grove;
  4. use XML::Grove::Sub;
  5.  
  6. my $doc;
  7.  
  8. my $filter = new Sub;
  9.  
  10. foreach $doc (@ARGV) {
  11.     my $parser = XML::Parser->new(Style => 'grove');
  12.     $parser->parsefile ($doc);
  13.     my $grove = $parser->{Grove};
  14.  
  15.     # this sub returns all the elements with a name containing the letter `d'.
  16.     my $sub = sub {
  17.     my ($object) = @_;
  18.  
  19.     if (ref($object) =~ /::Element/
  20.         && $object->{Name} =~ /d/i) {
  21.         return ($object);
  22.     }
  23.  
  24.     return ();
  25.     };
  26.         
  27.     my @matches = $grove->filter($sub);
  28.     my $match;
  29.     foreach $match (@matches) {
  30.     # prints the element name of the match.
  31.     print $match->{Name} . "\n";
  32.     }
  33. }
  34.