| 1 | # $Id: Objects.pm,v 1.1 2003/08/18 20:20:51 matt Exp $ |
| 2 | |
| 3 | package XML::Parser::Style::Objects; |
| 4 | use strict; |
| 5 | |
| 6 | sub Init { |
| 7 | my $expat = shift; |
| 8 | $expat->{Lists} = []; |
| 9 | $expat->{Curlist} = $expat->{Tree} = []; |
| 10 | } |
| 11 | |
| 12 | sub Start { |
| 13 | my $expat = shift; |
| 14 | my $tag = shift; |
| 15 | my $newlist = [ ]; |
| 16 | my $class = "${$expat}{Pkg}::$tag"; |
| 17 | my $newobj = bless { @_, Kids => $newlist }, $class; |
| 18 | push @{ $expat->{Lists} }, $expat->{Curlist}; |
| 19 | push @{ $expat->{Curlist} }, $newobj; |
| 20 | $expat->{Curlist} = $newlist; |
| 21 | } |
| 22 | |
| 23 | sub End { |
| 24 | my $expat = shift; |
| 25 | my $tag = shift; |
| 26 | $expat->{Curlist} = pop @{ $expat->{Lists} }; |
| 27 | } |
| 28 | |
| 29 | sub Char { |
| 30 | my $expat = shift; |
| 31 | my $text = shift; |
| 32 | my $class = "${$expat}{Pkg}::Characters"; |
| 33 | my $clist = $expat->{Curlist}; |
| 34 | my $pos = $#$clist; |
| 35 | |
| 36 | if ($pos >= 0 and ref($clist->[$pos]) eq $class) { |
| 37 | $clist->[$pos]->{Text} .= $text; |
| 38 | } else { |
| 39 | push @$clist, bless { Text => $text }, $class; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | sub Final { |
| 44 | my $expat = shift; |
| 45 | delete $expat->{Curlist}; |
| 46 | delete $expat->{Lists}; |
| 47 | $expat->{Tree}; |
| 48 | } |
| 49 | |
| 50 | 1; |
| 51 | __END__ |
| 52 | |
| 53 | =head1 NAME |
| 54 | |
| 55 | XML::Parser::Style::Objects |
| 56 | |
| 57 | =head1 SYNOPSIS |
| 58 | |
| 59 | use XML::Parser; |
| 60 | my $p = XML::Parser->new(Style => 'Objects', Pkg => 'MyNode'); |
| 61 | my $tree = $p->parsefile('foo.xml'); |
| 62 | |
| 63 | =head1 DESCRIPTION |
| 64 | |
| 65 | This module implements XML::Parser's Objects style parser. |
| 66 | |
| 67 | This is similar to the Tree style, except that a hash object is created for |
| 68 | each element. The corresponding object will be in the class whose name |
| 69 | is created by appending "::" and the element name to the package set with |
| 70 | the Pkg option. Non-markup text will be in the ::Characters class. The |
| 71 | contents of the corresponding object will be in an anonymous array that |
| 72 | is the value of the Kids property for that object. |
| 73 | |
| 74 | =head1 SEE ALSO |
| 75 | |
| 76 | L<XML::Parser::Style::Tree> |
| 77 | |
| 78 | =cut |