Class::XML::Parser

Parses (and optionally validates against a DTD) an XML message into a user-defined class structure
Download

Class::XML::Parser Ranking & Summary

Advertisement

  • Rating:
  • License:
  • Perl Artistic License
  • Price:
  • FREE
  • Publisher Name:
  • Mark Morgan
  • Publisher web site:
  • http://search.cpan.org/~mmorgan/

Class::XML::Parser Tags


Class::XML::Parser Description

Parses (and optionally validates against a DTD) an XML message into a user-defined class structure Class::XML::Parser is a Perl module that allows for XML to be parsed into an user-defined object hierarchy. Additionally, the XML will be validated against it's DTD, if such is defined within the XML body, and XML::Checker::Parser is available.A note as to how the parsing is done. When the ->parse method is called, the each element name is checked against the current class' (root_class by default) __xml_parse_objects result. If an entry exists for this element in the __xml_parse_objects hash, a new instance of the destination class is created. All further elements and attributes will be called as mutators on that object, until the closing tag for the element is found, at which time the previous object would be restored, and all further elements will default to calling accessors on that object. If nested elements are found, but no __xml_parse_objects definition exists for them, any data elements and attributes will be folded with the current object (container-only elements are *not* added).SYNOPSIS # parse result base class, just defines an autoloader package ParseResult::Base; sub new { bless {}, shift(); } sub AUTOLOAD { my ( $self, $val ) = @_; my $meth = $AUTOLOAD; $meth =~ s/.*:://; return if $meth eq 'DESTROY'; if ( defined $val ) { $self->{ $meth } = $val; } $self->{ $meth }; } # define classes that xml gets parsed into package ParseResult; use base qw( ParseResult::Base ); # optionally define sub-classes that specific elements will be parsed into. # If this method doesn't exist, then all sub-elements and attributes thereof # will be parsed into this class sub __xml_parse_objects { { blah => 'ParseResult::Blah', } } # optionally, have a class use a constructor other than 'new'. Useful # for Class::Singleton objects sub __xml_parse_constructor { 'new' } # optionally, have elements aliased to a method other than the XML # element name sub __xml_parse_aliases { { elem1 => 'bar', } } package ParseResult::Blah; use base qw( ParseResult::Base ); package main; use Class::XML::Parser; my $xml = 'ParseResult', # top-level class to parse results into prune => 1, validate => 1, # DTD validation should be done map_uri => { # maps from XML SYSID or PUBID to URLs to replace. Use to avoid # having to do a HTTP retrieval of the DTD, instead finding it on # the local filesystem 'http://example.com/parse.dtd' => 'file:/tmp/parse.dtd', }, ); my $top = $parser->parse( $xml ) or die $parser->last_error; print Dumper $top; # assuming the DTD exists, this will return a structure of: #$VAR1 = bless( { # 'blah' => bless( { # 'wibble' => 'wobble', # sub-element of # 'a' => '20' # attributes are also handled # }, 'ParseResult::Blah' ), # created as new object, as blah # # defined in higher-level # # __xml_parse_objects # 'qwerty' => 'uiop' # sub-element of root #}, 'ParseResult' ); # top object is blessed into # # 'root_class' Requirements: · Perl


Class::XML::Parser Related Software