For you two perl XML::Parser fans, here's another XML::Parser style to parse the private message XML ticker. Remember, you need log in your user-agent before you pick up your messages and you need to escape the '&'.

This is the companion to the Perl E2 chatterbox XML ticker parser. Together they could make a little chatterbox client.

POD at the end.

package E2Chatterbox::MsgParser;

use strict;

########################################################################
########################################################################
###
### XML subs
###
########################################################################
########################################################################
sub Init {
    my $expat = shift();
                        
    $expat->{speaker} = undef;
    $expat->{message} = undef;
    $expat->{message_id} = undef;
    $expat->{time} = undef;
    $expat->{msg} = [];
}
 
 
sub Char {
    my ( $expat, $string ) = @_;
                                
    if ( defined($expat->{speaker}) ) {
        $string =~ s/\n//g;

        $expat->{message} .= $string;
    }                                   
}
 
sub Start {
    my ( $expat, $element, %attr ) = @_;
    $element = lc( $element );          
                              
    if ( $element eq 'message' ) {
        $expat->{speaker}    = $attr{author};
        $expat->{time}       = $attr{time};
        $expat->{message_id} = $attr{message_id};
    }                                               
    elsif ( $element eq 'a' ) {
        $expat->{message} .= '[';
    }                                   
}
 
sub End {
    my ( $expat, $element ) = @_;
    $element = lc( $element );   
                              
    if ( $element eq 'message' ) {
        push( @{$expat->{msg}}, [ $expat->{time}, $expat->{speaker}
,  
            $expat->{message}, $expat->{message_id} ] );

        $expat->{speaker} = undef;
        $expat->{message} = undef;
        $expat->{time} = undef;
    }
    elsif ( $element eq 'a' ) {
        $expat->{message} .= ']';
    }
}

sub Final {
    my $expat = shift();

    return $expat->{msg};
}

1;

=pod

=head1 NAME

E2ChatterbxMParser - An xml parser for E2 private messages

=head1 SYNOPSIS

 use XML::Parser;

 my $parser = new XML::Parser (
   Style => "E2Chatterbox::MsgParser"
 );
   
 my ( $messages ) = $parser->parse( $xml );

 foreach my $line ( @$messages ) {
   my ( $time, $speaker, $speech, $id ) = @$line;
   print "[$time/$id] $speaker: $speech\n";
 }

=head1 DESCRIPTION

This is a style for the XML::Parser perl module to deal with
private messages from E2 (/?node=private+message+XML+ticker).
It works by taking the xml it is passed and returning a reference
to an array of arrays.

=cut

Log in or register to write something here or to contact authors.