XML, the Perl Way

Processing XML with Perl Michel Rodriguez

Examples: PYX one-liners

Example: extracting information from an XML document

Example: word count

Counting the number of words in an XML document.

#!/bin/perl -w
use strict;

my $nbw=0;

foreach my $file (@ARGV)
  { open( XML, "pyx $file |") or die "cannot open file $file: $!";
    while( <XML>)
      { next unless m/^-/;   # skip markup
        next if( m/^-\\n$/); # skip line returns
        my @words= split;    # get the words
        $nbw+= @words;       # get the number of words in the line
      }
    # When opening from a pipe some errors only show at close time
    close XML or die "error with pyx $file: $!";;
  }
print $nbw, " words\n";


Examples: PYX one-liners

Example: extracting information from an XML document