Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

XML::Simple chatterbox dumper

by jeffa (Bishop)
on Aug 16, 2001 at 02:18 UTC ( [id://105211] : CUFP . print w/replies, xml ) Need Help??

Inspired by OeufMayo's XML::Parser Tutorial many months ago, i wrote a similar version with XML::Simple. Since then, the computer the script resided on crashed, and i had to rewrite it. So, i present it here in the hopes that when my new computer crashes, i can get a copy here . . . um, yeah.

Update: (Aug 20, 2001)
added 'forcearray' option to handle edge conditions (no messages, one message)

use strict; use XML::Simple; use LWP::Simple; use constant URL => q| +l+ticker|; $| = 1; my $cb_ticker = get(URL) or die "site down!"; my $xml = XMLin($cb_ticker,forcearray=>1) or die "xml error!"; foreach my $msg (@{$xml->{'message'}}) { my $auth = $msg->{'author'} || ''; my $cont = $msg->{'content'} || ''; my $time = $msg->{'time'} || ''; $time = join(":", $time =~ /^\d{8}(\d\d)(\d\d)(\d\d)$/) if $time; $cont =~ s/\n//g; $cont =~ s/^\/me/$auth/; print "$time - [$auth] $cont\n"; }

Replies are listed 'Best First'.
Re: XML::Simple chatterbox dumper
by mirod (Canon) on Aug 16, 2001 at 10:04 UTC

    And you thought I'd let get away without forcing an XML::Twig version upon you?

    Of course you need a recent (3.00.18 or above) version of the module in order to get the parseurl method, otherwise you will need to use LWP to get the text first.

    #!/bin/perl -w use strict; use XML::Twig; use constant URL => q{ +l+ticker}; my $t= XML::Twig->new( twig_handlers => { message => \&display_message + }); $t->parseurl( URL); sub display_message { my( $t, $msg)= @_; my $auth = $msg->att( 'author'); my $cont = $msg->text; my $time = $msg->att( 'time'); my ($h,$m,$s) = $time =~ /^\d{8}(\d\d)(\d\d)(\d\d)$/; $cont =~ s/\n//g; $cont =~ s/^\/me/$auth/; print "$h:$m:$s - [$auth] $cont\n"; }