Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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="" 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

In reply to error in perl script while processing xml data. by veerubiji

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others studying the Monastery: (2)
    As of 2018-08-18 10:08 GMT
    Find Nodes?
      Voting Booth?
      Asked to put a square peg in a round hole, I would:

      Results (185 votes). Check out past polls.