XML::Saxtract - Streaming parse XML data into a result hash based upon a specification hash
version 1.00
use XML::Saxtract qw(saxtract_string saxtract_uri); my $xml = "<root id='1' />"; my $spec = { '/root/@id' => rootId }; my $result = saxtract_string( $xml, $spec ); my $rootId = $result->{rootId}; my $complex_xml = <<'XML'; <root xmlns='http://abc' xmlns:d='http://def' d:id='1' name='root' d:other='abc'> <person id='1'>Lucas</person> <d:employee id='2'>Ali</d:employee> <person id='3'>Boo</person> <d:employee id='4'>Dude</d:employee> </root> XML # get a map of all the employees my $complex_spec = { 'http://def' => 'k', '/root/k:employee' => { name => 'employees', type => 'map', key => 'name', spec => { '' => sub { my ($object, $value) = @_; $object->{name} => $value; $object->{email} => lc($value) . '@example.com'; }, '/@id' => 'id' } } }; my $result = saxtract_string( $complex_xml, $complex_spec ); foreach my $employee ( keys( %{$result->{employees}} ) ) { print( "$employee->{id}: $employee->{name} <$employee->{email}>\n" ); } # Prints: # 2: Ali <ali@example.com> # 4: Dude <dude@example.com>
This module provides methods for SAX based (streaming) parsing of XML data into a result hash based upon a specification hash.
Lucas Theisen <lucastheisen@pastdev.com>
This software is copyright (c) 2013 by Lucas Theisen.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install XML::Saxtract, copy and paste the appropriate command in to your terminal.
cpanm
cpanm XML::Saxtract
CPAN shell
perl -MCPAN -e shell install XML::Saxtract
For more information on module installation, please visit the detailed CPAN module installation guide.