Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

error in perl script while processing xml data.

by veerubiji (Sexton)
on Dec 14, 2011 at 17:46 UTC ( [id://943593]=perlquestion: print w/replies, xml ) Need Help??

veerubiji has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks, I am processing xml files form folders in a directory. I written script like this

#!/usr/bin/perl use warnings; use strict; use XML::Simple; use Carp; use File::Find; use File::Spec::Functions qw( canonpath ); use Data::Dumper; my @ARGV ="C:/Main/work";die "Need directories\n" unless @ARGV; find( sub { return unless ( /(_service\.xml)$/ and -f ); extract_info(); return; }, @ARGV ); sub extract_info { my $path= $_; my $xml=XMLin($path); my $xmldata_services = $xml->{Service}; my %dataservices; for my $xmldata_service (@$xmldata_services) { my %service = ( description => $xml_service->{Des}, name => $xml_service->{Name}, num => $xml_service->{NUM}, ); $service{sw} = _maybe_list( $xmldata_service->{Customers}{Softwar +e} ); $service{hw} = _maybe_list( $xml_service->{Suppliers}{Hardware} ); $service{sw} = _maybe_list( $xml_service->{Suppliers}{Software} ); $dataservices{ $service{id} } = \%service; } print Dumper \%dataservices; } sub _maybe_list { my $maybe = shift; return ref $maybe eq 'ARRAY' ? $maybe : [$maybe]; }

I getting one error when running this code like this "Not an array Reference" at for loop line.I tried in different ways to overcome this but still same error. How to overcome this error and How to get each file path and save in a hash? I am not able to rectify this and adding file path into hash.any one help me with this.

my xml look like this

<?xml version="1.0" encoding="UTF-8"?> <Servicelist xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi +:noNamespaceSchemaLocation="file:///files/service.xsd"> <Service NUM="B7a" Name="temperature sensor"> <Des>It delivers actual temperature in the form ov Volts</Des> <Customers> <Software Service="ADC" Path="/main/ADCservice.xml"/> </Customers> <Suppliers> <Software Service="input" Path="/main/inputservice.xml"/> <Software Service="signal" Path="/main/signalservice.xml"/> <Hardware Type="engine" Nr="18" Servicenum="1" Path="/main/engines +ervice.xml"/> <Hardware Type="motor" Nr="7" Servicenum="1" Path="/main/motorser +vice.xml"/> <Hardware Type="supply" Nr="1" Servicenum="1" Path="/main/supplyser +vice.xml"/> </Suppliers> </Service> </Servicelist>

any help appriciated

Thanks in advance

Replies are listed 'Best First'.
Re: error in perl script while processing xml data.
by Corion (Patriarch) on Dec 14, 2011 at 17:51 UTC

    Read the documentation of the modules you are using. See XML::Simple, especially the discussion of ForceArray.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://943593]
Approved by philipbailey
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2024-04-24 03:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found