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

Hey Perl Monks,
I have XML file that I am parsing and my task is to be able to access individual fields. I am using a dumper to report out following values. This is my XML output after parsing:
$VAR1 = { 'Data1' => { 'Data2' => { 'DATE' => '2000-10-19', 'ID' => 'A95', 'STATUS' => 'A', 'TIME' => {}, }, 'TIMESTAMP' => '2012-08-04T20:15:04.506-04:00' } };
And this is my other code that I have to get to 'Date" field:
my $val = $config->{'Data1'}->{'DATE'}; or my $val = $config->{'Data1'}->{'Data2'}->{'DATE'};
I have tried both of those and other variations but it does not print out anything. My goal is to print out just '2000-10-19'. Am I doing something wrong?
Thanks in advance for your help!

Replies are listed 'Best First'.
Re: Perl XML parsing with XML::Simple - Having problem with accessing fields
by roboticus (Chancellor) on Aug 17, 2012 at 15:57 UTC

    vparikh:

    It works just fine for me. Perhaps you're doing someting to disturb your hash before you try to print it?

    $ cat t.pl #!/usr/bin/perl use strict; use warnings; my $VAR1; $VAR1 = { 'Data1' => { 'Data2' => { 'DATE' => '2000-10-19', 'ID' => 'A95', 'STATUS' => 'A', 'TIME' => {}, }, 'TIMESTAMP' => '2012-08-04T20:15:04.506-04:00' } }; print $VAR1->{Data1}{Data2}{DATE}, ".\n"; print $VAR1->{Data1}->{Data2}->{DATE}, ".\n"; $ perl t.pl 2000-10-19. 2000-10-19.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      Thank you for your quick reply. But I am not able to access $var1 as a variable since it is not really in my script. My original script is this:
      use XML::Simple; # qw(:strict); use Data::Dumper; my $simple = XML::Simple->new(); my $config = $simple->XMLin('/home/parse3.xml'); print $config; print $config->{Data1}->{Data2}->{DATE}, ".\n";
      Before I was using Data dumper, so it would dump it all out as $VAR1. So my question is how do I access the date field with or without data dumper? Thanks again. I really appreciate your help!

        Since you didn't provide your actual XML, I had to dummy some up based on the dump you provided.

        <root> <Data1> <TIMESTAMP>2012-08-04T20:15:04.506-04:00</TIMESTAMP> <Data2> <DATE>2000-10-19</DATE> <ID>A95</ID> <STATUS>A</STATUS> <TIME></TIME> </Data2> </Data1> </root>

        Using a slightly modified version of your script, I'm able to print the date.

        use strict; use warnings; use XML::Simple; # qw(:strict); use Data::Dumper; my $simple = XML::Simple->new(); my $config = $simple->XMLin('parse3.xml'); # /home/parse3.xml isn't va +lid on my system #print Dumper( $config ); # Uncomment to get a dump of the data struct +ure print $config->{Data1}->{Data2}->{DATE}, ".\n"; __END__ Output: 2000-10-19.

        vparikh:

        The same way, just use your variable name ($config) instead of $VAR1 in your code. I used $VAR1 because I just used the data structure from your post and wanted to minimize changes so you could see how to do it.

        ...roboticus

        When your only tool is a hammer, all problems look like your thumb.

A reply falls below the community's threshold of quality. You may see it by logging in.