XML technologies / XML / Perl & XML / Tree-oriented processing / XML::Grove

(Excerpt from "Perl & XML" by Erik T. Ray & Jason McIntosh)

Before returning to standards-based solutions, let us examine one final object model, Ken MacLeod’s XML::Grove. Like XML::SimpleObject, the module uses output from XML::Parser in tree mode and converts it to its own object structure. Each node type is represented by a separate class. An element is an instance of the class XML::Grove::Element; a processing instruction corresponds to an instance of the class XML::Grove::PI and so on. Text remains represented by scalar values.

One advantage is that internal subset declarations are stored in lists accessible through the XML::Grove object. Each entity and notation remains available for later use. For example, the following program counts occurrences of elements and other nodes, then prints a list of node types and their counts.

First, we initialize the parser with the style “grove”. This tells XML::Parser to process its output using the module XML::Parser::Grove:

use XML::Parser;
use XML::Parser::Grove;
use XML::Grove;

my $parser = XML::Parser->new( Style => 'grove', NoExpand => '1' );
my $grove = $parser->parsefile( shift @ARGV );

Next, we access the document content using the method contents( ) of the object $grove. The result is a list of nodes including the root element and any comments or processing instructions outside it. A subroutine tabelle( ) counts nodes, recursively including nodes of child elements. Finally, the result is printed:

# Tabelle über Elemente und andere Knoten erstellen
my %dist;
foreach( @{$grove->contents} ) {
   &tabulate( $_, \%dist );
}
print "\nGefundene Knoten:\n\n";
foreach( sort keys %dist ) {
   print "$_: " . $dist{$_} . "\n";
}

The counting subroutine follows. Since nodes have different types, we use ref( ), to determine the type. Attributes are not counted as nodes here, but are available through the element class’s method attributes( ) as a hash. Calling the method contents( ) allows child elements to be processed in the same way.

# Ein Knoten und eine Tabelle werden übergeben. Finde heraus, um was für
# einen Knoten es sich handelt, und erhöhe den entsprechenden Zähler.
# Rekursiver Aufruf, falls notwendig
#
sub tabelle {   
   my( $node, $table ) = @_;
  
   my $type = ref( $node );
   if( $type eq 'XML::Grove::Element' ) {
      $table->{ 'element' }++;
      $table->{ 'element (' . $node->name . ')' }++;
      foreach( keys %{$node->attributes} ) {
      $table->{ "attribut ($_)" }++;
      }
      foreach( @{$node->contents} ) {
      &tabelle( $_, $table );
      }
     
   } elsif( $type eq 'XML::Grove::Entity' ) {
      $table->{ 'entity-ref (' . $node->name . ')' }++;
     
   } elsif( $type eq 'XML::Grove::PI' ) {
      $table->{ 'PI (' . $node->target . ')' }++;
     
   } elsif( $type eq 'XML::Grove::Comment' ) {
      $table->{ 'comment' }++;
     
   } else {
      $table->{ 'text-node' }++
   }
}

Here is typical output from running the program on an XML file:

Gefundene Knoten:
PI (a): 1
attribut (date): 1
attribut (style): 12
attribut (type): 2
element: 30
element (category): 2
element (inventory): 1
element (item): 6
element (location): 6
element (name): 12
element (note): 3
text-node: 100

  

  

 

 

 

back to top