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

@choroba: Yes, when I tried like that even I am getting the correct output as you showed. But actually I am dumping from $xml where I have the actual xml file using following lines. My apologies for not being clear about this before. Also in other parts of code I am able to fetch data by referencing $xst

@NetWallah: I am using this for dumping the xml

my $xs = XML::Simple->new(); $xst = eval { $xs->XMLin($xml,KeyAttr=>1) }; &printXMLErr($@) if($@); print "Value of \$xst is:\n"; print Dumper($xst);
Here print Dumper($xst); shows all the data from xml converted into the format I provided. And I am able to fetch data using reference to these like: $xst->{cmts}->{Pre_EQ}->{groupDelayMag}->{content}; I think the problem I am having is about the elements that are in [] (array of hash?) displayed in $xst

Replies are listed 'Best First'.
Re^3: Problem in reading parsed xml using XML::Simple
by choroba (Cardinal) on Jun 24, 2015 at 15:20 UTC
    Try specifying the index of the array element in square brackets:
    print $xml->{cmts}{STBDSG}{dsg}[0]{tag1};
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      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} );