XML, the Perl Way

Processing XML with Perl Michel Rodriguez

XML::Parser Handlers

Example: extracting information from an XML document

Things to know about XML::Parser

Catching exceptions

The XML recommendation mandates that when an error is found in the XML the parser stop processing immediatly. XML::Parser goes even further: it displays an error message and then die's.

To avoid dying wrap the parse in an eval block:

  eval { $parser->parse };
  if( $@)
    { my $error= $@;
      #cleanup
    }

Getting all the character data

The Char handler can be called several times within a single text element. This happens when the text includes new lines, entities or even at random, depending on expat buffering mechanism. So the real content should actually be built by pushing the string passed to Char, and by using it only in the End handler.

my $stored_content='';             # global

sub Start
  { my( $expat, $gi, %atts)= @_;
    process( $stored_content);     # you might need this for mixed content
    $stored_content='';            # needs to be reset 
  }

sub Char
  { my( $expat, $string)= @_;
    $stored_content .= $string;    # can't do much with it
  }

sub End
  { my( $expat, $gi)= @_;
    
    process( $stored_content);     # now it's full
    $stored_content='';            # reset here too
  }


XML::Parser Handlers

Example: extracting information from an XML document