Stream XML::Parser : SAX « XML « Perl






Stream XML::Parser

  

use XML::Parser;

my $parser = XML::Parser->new( Handlers => {Start=>\&handle_start,End=>\&handle_end,});
$parser->parsefile( "yourXML.xml" );

my @element_stack;          # remember which elements are open

sub handle_start {
    my( $expat, $element, %attrs ) = @_;

    my $line = $expat->current_line;

    print "$element starting on # $line!\n";

    push( @element_stack, { element=>$element, line=>$line });

    if( %attrs ) {
        print "Attributes:\n";
        while( my( $key, $value ) = each( %attrs )) {
            print "\t$key => $value\n";
         }
    }
}

sub handle_end {
    my( $expat, $element ) = @_;

    my $element_record = pop( @element_stack );
    print "$element started on # ", $$element_record{ line };
}

   
    
  








Related examples in the same category

1.Check error in SAX parser
2.Check node name in SAX paser
3.Register handlers to SAX parser
4.SAX parser handler
5.Converting a comma separated list data source to XML
6.The XML::Parser module provides a framework for parsing XML.
7.Using XML::Parser to parse xml file
8.Using XML:Simple to read and store the document
9.XML::Parasr style: Tree