in reply to Basic parsing XML perl - using FOREACH
Hello,
You really must use strict; and use warnings; in your programs, otherwise you are rejecting Perl's built-in safety features, and there's not much point in helping when a programmer is doing that.
I believe you are looking for the ForceArray option:
Output:#!/usr/bin/perl use strict; use warnings; use XML::Simple qw/ XMLin /; use Data::Dumper; $Data::Dumper::Sortkeys = 1; local $/; my $staff = XMLin( <DATA>, ForceArray => 1 ); foreach my $employee ( @{ $staff->{'employee'} } ) { print Dumper( $employee ); } __DATA__ <?xml version='1.0'?> <staff> <employee> <name>Pradeep</name> <age>23</age> <sex>M</sex> <department>Coder</department> </employee> <employee> <name>Pradeep</name> <age>22</age> <sex>M</sex> <department>HR</department> </employee> </staff>
( Note that many monks recommend using XML::Twig or XML::LibXML instead, see the documentation to XML::Simple itself. )$VAR1 = { 'age' => [ '23' ], 'department' => [ 'Coder' ], 'name' => [ 'Pradeep' ], 'sex' => [ 'M' ] }; $VAR1 = { 'age' => [ '22' ], 'department' => [ 'HR' ], 'name' => [ 'Pradeep' ], 'sex' => [ 'M' ] };
Hope this helps!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Basic parsing XML perl - using FOREACH
by gr.d (Novice) on Dec 30, 2015 at 07:09 UTC | |
by 1nickt (Canon) on Dec 30, 2015 at 07:30 UTC | |
|
Re^2: Basic parsing XML perl - using FOREACH ( warnings; )
by Anonymous Monk on Apr 08, 2016 at 03:16 UTC |