gr.d has asked for the wisdom of the Perl Monks concerning the following question:

Below is a XML file

<?xml version='1.0'?> <employee> <name>Pradeep</name> <age>23</age> <sex>M</sex> <department>Coder</department> </employee>

And the perl code is

use XML::Simple; use Data::Dumper; $xml=new XML::Simple; $data=$xml->XMLin("data.xml"); print Dumper($data);

Now how do you parse if the XML file is

<?xml version='1.0'?> <employee="risc_31> <name>John Doe</name> <age>43</age> <sex>M</sex> <department>Analyst</department> </employee> <employee="risc_32> <name>Pradeep</name> <age>23</age> <sex>M</sex> <department>HR</department> </employee>

how can this be done using a foreach loop in perl NOTE: XML::Simple is easier for me Any help is appreciated!

Replies are listed 'Best First'.
Re: Basic parsing XML perl - using FOREACH
by 1nickt (Canon) on Dec 30, 2015 at 06:28 UTC

    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:

    #!/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>
    Output:
    $VAR1 = { 'age' => [ '23' ], 'department' => [ 'Coder' ], 'name' => [ 'Pradeep' ], 'sex' => [ 'M' ] }; $VAR1 = { 'age' => [ '22' ], 'department' => [ 'HR' ], 'name' => [ 'Pradeep' ], 'sex' => [ 'M' ] };
    ( Note that many monks recommend using XML::Twig or XML::LibXML instead, see the documentation to XML::Simple itself. )

    Hope this helps!


    The way forward always starts with a minimal test.

      Thank you, I have another question though

      what if

      <employee="risc_30"> <name>Pradeep</name> <age>23</age> <sex>M</sex> <department>Coder</department> </employee> <employee="risc_31"> <name>Pradeep</name> <age>22</age> <sex>M</sex> <department>HR</department> </employee>

      how does the foreach loop work then

        That's not valid XML, so the parser will bork.


        The way forward always starts with a minimal test.

      when you say "you must use strict; and use warnings; "

      you must enumerate the problems in OPs code that would be caught by use strict; use warnings;

Re: Basic parsing XML perl - using FOREACH
by Laurent_R (Canon) on Dec 30, 2015 at 07:28 UTC
    Not a direct answer to your question, but you probably want to use another module for parsing your XML. You might want to look at what other monks have to say about XML::Simple: http://www.perlmonks.org/?node_id=1150844.