use strict;
use warnings;
use 5.014;
use XML::LibXML;
my $filename = "xml.xml";
my $parser = XML::LibXML->new();
my $doc = $parser->parse_file($filename);
my %results;
for my $name ($doc->findnodes('/names/name')) {
my $num = $name->getAttribute('number');
for my $school ($name->findnodes('description/schooldata')) {
push @{$results{$num}}, $school->getAttribute('file');
}
}
use Data::Dumper;
say Dumper(\%results);
--output:--
$VAR1 = {
'6' => [
'/home/betty/.01',
'/home/betty/.02',
'/home/betty/0.3',
'/home/betty/0.4'
],
'5' => [
'/home/matt/.01',
'/home/matt/.02',
'/home/matt/0.3',
'/home/matt/0.4'
]
};
xml.xml:
<?xml version="1.0" encoding="UTF-8"?>
<names>
<name number="5">
<description>
<schooldata file="/home/matt/.01"></schooldata>
</description>
<description>
<schooldata file="/home/matt/.02"></schooldata>
</description>
<description>
<schooldata file="/home/matt/0.3"></schooldata>
</description>
<description>
<schooldata file="/home/matt/0.4"></schooldata>
</description>
</name>
<name number="6">
<description>
<schooldata file="/home/betty/.01"></schooldata>
</description>
<description>
<schooldata file="/home/betty/.02"></schooldata>
</description>
<description>
<schooldata file="/home/betty/0.3"></schooldata>
</description>
<description>
<schooldata file="/home/betty/0.4"></schooldata>
</description>
</name>
</names>