A
France
1989
001
Italy
1991
002
Germany
1995
003
B
004
005
C
####
package House
{
use XML::Rabbit::Root;
has_xpath_object_list depts => '/root/departments/department' =>
'House::Depts';
finalize_class();
}
package House::Depts
{
use XML::Rabbit;
has_xpath_object_list articles => './articles/article[key]|./articles/article/extra' => 'House::Article';
has_xpath_value name => './name';
finalize_class();
}
package House::Article
{
use XML::Rabbit;
has_xpath_value key => './key|./extrakey';
has_xpath_object prod => './production' => 'Article::Production';
finalize_class();
}
package Article::Production
{
use XML::Rabbit;
has_xpath_value country => './country';
has_xpath_value year => './year';
finalize_class();
}
1;
##
##
#!/perl
use strict;
use warnings FATAL => qw(all);
use Encode;
use FindBin qw($Bin);
use lib $Bin;
use testlib;
use Try::Tiny;
my $path = shift or die "No source!\n";
my $string = do { local $/ = undef;
open my $in, "<", "$path" or die "$!";<$in>;};
my $xml = decode('utf8', $string);
my $h = House->new(xml => $xml);
for my $d ( @{$h->depts} )
{
for my $art ( @{$d->articles} )
{
my $prod = try {$art->prod} catch {0}; # $art->prod // 0;
my $country = $prod ? $prod->country : '';
print join("\t", $d->name, $art->key, $country), $/;
}
}
##
##
A 001 France
A 002 Italy
A 003 Germany
B 004
B 005