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

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 :

Replies are listed 'Best First'.
Re^5: Problem in reading parsed xml using XML::Simple
by tangent (Parson) on Jun 24, 2015 at 16:17 UTC
    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 :-)
        Try working back up the tree one step at a time to see what's happening:
        print Dumper ( $xst->{cmts}{STBDSG} ); then print Dumper ( $xst->{cmts} );
        Can you put some sample files up somewhere else - e.g. Dropbox - I could then have a look.
Re^5: Problem in reading parsed xml using XML::Simple
by choroba (Cardinal) on Jun 24, 2015 at 16:16 UTC
    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
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ