XML hacking (take 2)

gav on 2002-07-17T01:49:53

I'm sure I already posted this at work before I left but when I got home I found I hadn't. I guess I must have forgot to hit submit or something :)

Well today I did a bit of XML hacking for the first time in a while. Usually I use XML::Simple as it is a no brainer, but this time I'm dealing with big files and I thought I it would be a good excuse to learn XML::Twig. I think mirod has done a fantastic job on this module.

Such a good job that my XML processing is basically:

use XML::Twig;
my @orders = ();

sub handle_order {
    my ($twig, $order) = @_;
    my $tax = $order->get_xpath('./Total/Line[@type="Tax"]', 0);
    if ($tax && $tax->text > 0) {
        my $addr = $order->get_xpath('./AddressInfo[@type="ship"]', 0);
        push @orders, [(map $addr->field($_), qw(Zip City State)), $tax->text];
    }
    $twig->purge;
};

my $twig = XML::Twig->new(TwigHandlers => { Order => \&handle_order } );
$twig->parsefile('OrderList.xml');