Hi, I am sorry for my mistakes. Thanks for your suggestion.
I tried as you said but still problem is there, I am showing what I tried as below
#!/usr/bin/perl
use warnings;
use strict;
use Carp;
use File::Find;
use File::Spec::Functions qw( canonpath );
use XML::LibXML::Reader;
use Data::Dumper;
my @ARGV ="c:/main/folder";
die "Need directories\n" unless @ARGV;
my $Number;
my %hash;
find(
sub {
my $file = $_;
# my $path = canonpath $File::Find::name;
my $path =$_;
return unless -f $path;
return unless $file =~ /(_service\.xml)$/;
extract_information($path, \%hash);
return;
},
@ARGV
);
sub extract_information {
my( $path, $hash)=@_;
my $ret = open my $xmlin, '<', $path;
unless ($ret) {
carp "Cannot open '$path': $!";
return;
}
my $reader = XML::LibXML::Reader->new(IO => $xmlin);
unless ($reader) {
carp "Cannot create reader using '$path'";
return;
}
while ( $reader->nextElement( 'Service' )) {
my $Number = $reader->getAttribute( 'num');
$reader->nextElement( 'Suppliermodules' );
while ( $reader->nextElement( 'Hardwaremodule' )) {
my $module = {};
$module->{type} = $reader->getAttribute('Type');
$module->{nr} = $reader->getAttribute('Nr');
push @{$hash->{$Number}}, $module;
}
while ( $reader->nextElement( 'softwaremodule' )) {
my $module1 = {};
$module1->{service} = $reader->getAttribute('Service');
push @{$hash->{$Number}}, $module1;
}
$reader->nextElement( 'customermodules' );
while ( $reader->nextElement( 'softwaremodule' )) {
my $module2 = {};
$module2->{cusservice} = $reader->getAttribute('Service');
push @{$hash->{$Number}}, $module2;
}
}
close $xmlin
or carp "Cannot close '$path': $!";
return;
}
print Dumper \%hash;
I tried with find(\&wanted, @directories); to search directives But I have some difficulties with that so I fallowed my procedure.What I want to do is I need to create hash with service num as the key and and all information of software module and hardware module attributes in customer and supplier module elements are array of values for that key. here I have two elements customer and supplier modules so I think array of values in a hash value.So finally after running this script I need to look my hash like (Service num attribute as the key and remaining information as the array of values for that key. so I have all service nums as the keys and its information is array of values.) I tried like that in the above script but it doesn't give any errors.I am not getting exact output, it giving some element attributes and some service num's only.I am new to perl, I am not able to produce what I want, please help me with that.
If any typing errors is there please excuse me.
My output like this
'B7a'=>[
{
Service='ADC',
path='....'
}
{
Type='engine',
Nr='18',
Servicenum='1',
path='....'
}
{
Type='motor',
Nr='7',
Servicenum='1',
path='....'
}
{
Type='supply',
Nr='1',
Servicenum='1',
path='....'
}
{
Service='input',
path='----'
}
{
Service='signal',
path='----'
}
].....................
Thanks in advance.