in reply to Re^2: Problem in reading parsed xml using XML::Simple
in thread Problem in reading parsed xml using XML::Simple

Try specifying the index of the array element in square brackets:
print $xml->{cmts}{STBDSG}{dsg}[0]{tag1};
لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Replies are listed 'Best First'.
Re^4: Problem in reading parsed xml using XML::Simple
by Perl300 (Friar) on Jun 24, 2015 at 16:00 UTC
    Tried using something like
    my %dsg_tags; $dsg_tags{'tag1'} = $xst->{cmts}{STBDSG}{dsg}[0]{tag1}; foreach (sort keys %dsg_tags) { print "$_ : $dsg_tags{$_}\n"; }
    And still getting blank value for tag1 even though there actually is a value. There is a warning as well like:
    Use of uninitialized value in concatenation (.) or string at ./<script +_name>.pl line <>. tag1 :
      Again, your data is something else than you've showed us.
      #!/usr/bin/perl use warnings; use strict; my $xst = { 'cmts' => { 'STBDSG' => { 'dsg' => [ { 'tag1' => '1', 'tag2' => 'caSystemId', }, { 'tag1' => '2', 'tag2' => 'gaSystemId', } ] }}}; my %dsg_tags; $dsg_tags{'tag1'} = $xst->{cmts}{STBDSG}{dsg}[0]{tag1}; foreach (sort keys %dsg_tags) { print "$_ : $dsg_tags{$_}\n"; } __END__ Output: tag1 : 1
      لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      Try sprinkling lots of "print Dumper" statements to see what you really have in that structure:
      print Dumper ( $xst->{cmts}{STBDSG}{dsg} ); print Dumper ( $xst->{cmts}{STBDSG}{dsg}[0] ); print Dumper ( $xst->{cmts}{STBDSG}{dsg}[0]{tag1} );
        @choroba: I really wish I could put the actual file here but it's quite big and has different data each time as it is generated at runtime. :-( @tangent: I tried your suggestion and it gave following result: I added:
        print Dumper ( $xst->{cmts}{STBDSG}{dsg} ); print Dumper ( $xst->{cmts}{STBDSG}{dsg}[0] ); print Dumper ( $xst->{cmts}{STBDSG}{dsg}[0]{tag1} );
        Which gave:
        $VAR1 = [ {} ]; $VAR1 = {}; $VAR1 = undef;
        So I think problem is what choroba stated earlier that $xst->{cmts}{STBDSG}{dsg}[0]{tag1} is getting undef :-( I think I have to dig deep in this part of my code:
        my $xst; if((not defined $xml) or ($xml =~ m/read\stimeout/i)){ &printXMLErr('request timed out'); } else { my $xs = XML::Simple->new(); $xst = eval { $xs->XMLin($xml,KeyAttr=>1) }; &printXMLErr($@) if($@); }
        It seems that answer to why $xst->{cmts}{STBDSG}{dsg}[0]{tag1} is getting undef is known to either KeyAttr or forcearray (or both). I am trying to find it out from them :-)