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

I have one XML file

<MTPconfiguration> <lteNbrEnbInfo> <field record="lteNbrEnbInfo"> <key name="enbType" /> <value id="1" /> <key name="enbId"/> <value id="130560" /> <key name="plmnID" /> <value id="00101" /> </field> </lteNbrEnbInfo> </MTPconfiguration>

I am trying to access those multiple keys and values in a loop like the code which i was mentioned below, but it is showing error.

#!/usr/bin/perl use strict; use warnings; use XML::Simple; use Data::Dumper; my $XML = XMLin("C:\\Users\\Administrator\\Desktop\\Automation\\VERSIO +N2\\TC2\\value.xml"); my $MTPconfiguration=$XML->{MTPconfiguration}; my $lteNbrEnbInfo=$MTPconfiguration->{lteNbrEnbInfo}; my $Enbfield=$lteNbrEnbInfo->{field}; print Dumper($Enbfield); my @field = $Enbfield->{field}->{key}; print "@field \n" foreach (@($Enbfield->{field})) { print $_->{key}. "\n"; print $_->{value}. "\n"; }

Replies are listed 'Best First'.
Re: How to access multiple values from XML file in our perl file
by choroba (Cardinal) on Jan 09, 2015 at 12:35 UTC
    Dereference uses curly brackets:
    foreach (@{ $Enbfield->{field} }) { # ^ ^
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

      Even i tried that one by changing that braces. But it is also showing error as "syntax error at field.pl line 15, near ") {" syntax error at field.pl line 18, near "}" Execution of field.pl aborted due to compilation errors."

        Semicolon missing here perhaps ?
        print "@field \n"; # ^
        Consider using XML::Twig
        #!/usr/bin/perl use strict; use warnings; use XML::Twig; my $xml = 'value.xml'; my $twig = new XML::Twig( twig_handlers =>{ field => \&field } ); $twig->parsefile($xml); sub field { my ($t,$e) = @_; for ($e->children){ if ($_->gi eq 'key'){ print $_->att('name')." "; } elsif ($_->gi eq 'value'){ print $_->att('id')."\n"; } } }
        poj
Re: How to access multiple values from XML file in our perl file
by Corion (Patriarch) on Jan 09, 2015 at 12:04 UTC

    What error is it showing?

      it is showing the error as Scalar found where operator expected at field.pl line 15, near "@($Enbfield" (Missing operator before $Enbfield?) syntax error at field.pl line 15, near "@($Enbfield" syntax error at field.pl line 18, near "}" Execution of field.pl aborted due to compilation errors.

Re: How to access multiple values from XML file in our perl file
by Jenda (Abbot) on Jan 13, 2015 at 13:35 UTC

    Step 1) find the person that designed that XML

    Step 2) kick him or her repeatedly

    Step 3) get the format changed

    The list of <key> and <value> tags like this is a bad bad design.

    Jenda
    Enoch was right!
    Enjoy the last years of Rome.