3

Hi everyone,
This is very simple for perl programmers but not beginners like me,
I have one xml file and I processed using XML::Simple like this

my $file="service.xml";
my $xml = new XML::Simple;
 my $data = $xml->XMLin("$file", ForceArray =>  ['Service','SystemReaction',  
                                      'Customers', 'Suppliers','SW','HW'],);

Dumping out $data, it looks like this:

$data = {
   'Service' => [{
         'Suppliers' => [{
               'SW' => [
                  {'Path' => '/work/service.xml',  'Service' => 'b7a'},
                  {'Path' => '/work/service1.xml', 'Service' => 'b7b'},
                  {'Path' => '/work/service2.xml', 'Service' => 'b5'}]}
         ],
         'Id' => 'SKRM',
         'Customers' =>
            [{'SW' => [{'Path' => '/work/service.xml', 'Service' => 'ASOC'}]}],
         'Des'  => 'Control the current through the pipe',
         'Name' => ' Control unit'
      },
      {
         'Suppliers' => [{
               'HW' => [{
                     'Type'    => 'W',
                     'Path'    => '/work/hardware.xml',
                     'Nr'      => '18',
                     'Service' => '1'
                  },
                  {
                     'Type'    => 'B',
                     'Path'    => '/work/hardware.xml',
                     'Nr'      => '7',
                     'Service' => '1'
                  },
                  {
                     'Type'    => 'k',
                     'Path'    => '/work/hardware.xml',
                     'Nr'      => '1',
                     'Service' => '1'
                  }]}
         ],
         'Id' => 'ADTM',
         'Customers' =>
            [{'SW' => [{'Path' => '/work/service.xml', 'Service' => 'SDCR'}]}],
         'Des'  => 'It delivers actual motor speed',
         'Name' => ' Motor Drivers and Diognostics'
      },
      # etc.
   ],
   'Systemreaction' => [
      # etc.
   ],
};

How to access each elements in the service and systemReaction(not provided). because I am using "$data" in further processing. So I need to access each Id,customers, suppliers values in each service. How to get particular value from service to do some process with that value.for example I need to get all Id values form service and create nodes for each id values.

To get Type and Nr value I tried like this

 foreach my $service (@{ $data->{Service}[1]{Suppliers}[0]{HW}[0] }) {
 say $service->{Nr};
 }
 foreach my $service (@{ $data->{Service}[1]{Suppliers}[0]{HW}[0] }) {
 say $service->{Type};

  }

can you help me how to get all Nr and Type values from Supplier->HW.

1
  • I've updated my answer in response to your edit.
    – derobert
    Dec 19, 2011 at 17:16

1 Answer 1

8

I suggest reading perldocs Reference Tutorial and References and Nested Data Structures. They contain an introduction and full explanation of how to access data like that.

But, for example, you can access the service ID by doing:

say $data->{Service}[0]{Id}   # prints SKRM

You could go through all the services, printing their ID, with a loop:

foreach my $service (@{ $data->{Service} }) {
    say $service->{Id};
}

In response to your edit

$data->{Service}[1]{Suppliers}[0]{HW}[0] is an hash reference (you can check this quickly by either using Data::Dumper or Data::Dump on it, or just the ref function). In particular, it is { Nr => 18, Path => "/work/hardware.xml", Service => 1, Type => "W" }

In other words, you've almost got it—you just went one level too deep. It should be:

foreach my $service (@{ $data->{Service}[1]{Suppliers}[0]{HW} }) {
 say $service->{Nr};
}

Note the lack of the final [0] that you had.

4
  • Sorry for asking I gone through reference tutorial but still i am confusing getting "type" and "Nr" because its nested elements. these arrays and hashes makes me difficult. can you some suggestion how to get that elements.
    – verendra
    Dec 15, 2011 at 22:14
  • @verendra: In that dump, each { is introducing a new hashref, each [ a new arrayref. So one Nr would be $data->{Service}[1]{Suppliers}[0]{HW}[0]{Nr} ... also, you may find other XML modules easier to use; have you looked at XML::DOM for example?
    – derobert
    Dec 15, 2011 at 22:21
  • @verendra: Also, if you're having trouble understanding perlreftut or perlref, I'm sure SO would welcome specific questions about whichever parts are confusing/unclear/incomprehensible.
    – derobert
    Dec 15, 2011 at 22:33
  • I have small doubt again can you have a look at my edited post. I tried something like but its not working why?
    – verendra
    Dec 17, 2011 at 11:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.