in reply to Get XML content using XML::Twig module

You could write something like the code below, which is very simple but probably not easily extensible, or write at the Connection level, have just one twig_handler for the connection, extract whichever info you need from the tree for that element, then purge it and be done. The methods used in the code below should help you write a better version.

#!/usr/bin/perl use strict; use warnings; use XML::Twig; XML::Twig->new( twig_handlers => { Sender => sub { print "sender: ", + $_->text, "\n"; }, Receiver => sub { print "receiver: +", $_->text, "\n"; }, FileType => sub { print " file: ", + $_->first_child->tag, "\n"; $_->purge; }, }, ) ->parsefile( "fdata.xml");

Replies are listed 'Best First'.
Re^2: Get XML content using XML::Twig module
by Jenda (Abbot) on Apr 22, 2010 at 12:53 UTC

    Looks quite a bit like XML::Rules :-)

    use strict; use warnings; use XML::Rules; XML::Rules->new(rules => { Sender => sub { print "sender: ", $_[1]->{_ +content}, "\n"; }, Receiver => sub { print "sender: ", $_[1]->{_content}, "\n"; }, FileType => sub { delete $_[1]->{_content}; print " file: ", (keys( +%{$_[1]}))[0], "\n"; return; }, # it would work without the following rules, but is more efficient w +ith them 'InitTAP,FatalRAP,ReTxTAP' => 'content', '_default' => '', })->parsefile( "fdata.xml");

    Jenda
    Enoch was right!
    Enjoy the last years of Rome.