topical media & game development

talk show tell print

professional-xml-18-perl-streamcustomers.pl / pl



  use XML::Parser;
  
  my file = 'customers.xml';
  
  my parser = new XML::Parser();
  my \%cities;
  my flag = 0;
  
  sub start_handler {
          
          my p = shift;
          my elem = shift;
          
          if (elem =~ /city/) {                
                  flag = 1;
          }
          
  }
  
  sub end_handler {
          my p = shift;
          my elem = shift;
          if (elem =~ /customers/) {
                  
                  foreach city (keys %{cities}) {
                          print city, ": ", %{cities}->{city}, "\n";
                  }
          }
  }
  sub char_handler {
           if(flag) {
                   my (p, data) = @_;
                  cities->{data}++;
                   flag = 0;
          }
  }
  
  parser->setHandlers(Start => \&start_handler,
                       End   => \&end_handler,
                       Char  => \&char_handler);
    
  parser->parsefile(file);


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.