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

Hi all, I am using XML::Simple (can't use updated modules like XML::LibXML at this point) to read in a xml and get the values in variables to compare. The following element is giving me hard time though:
$VAR1 = { 'cmts' => { 'STBDSG' => { 'dsg' => [ { 'tag1' => '1', 'tag2' => 'caSystemId', }, { 'tag1' => '2', 'tag2' => 'gaSystemId', } ] },
I am trying to read values of tag1 and tag2 in distinct variables and have tried few options one of which is:
my $dsg_tags = $xst->{cmts}->{STBDSG}->{dsg}; for my $dsg (@$dsg_tags) { print "Value of tag1 :"; print $dsg->{tag1}, "\n"; print "Value of tag2 :"; print $dsg->{tag2}, "\n"; }
There are no compilation errors but nothing is printed when I run the script even though tag1, tag2 have values after xml is parsed. I am parsing xml using:
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($@); }
Can you please help me if I am missing something here and suggest me how to correct it. I am also trying to find from net and CPAN documentation. Thanks in advance!

Replies are listed 'Best First'.
Re: Problem in reading parsed xml using XML::Simple
by choroba (Cardinal) on Jun 23, 2015 at 22:47 UTC
    When I run your code, I'm getting the following output:
    Value of tag1 :1 Value of tag2 :caSystemId Value of tag1 :2 Value of tag2 :gaSystemId

    The exact code for the curious:

    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Problem in reading parsed xml using XML::Simple
by NetWallah (Canon) on Jun 24, 2015 at 03:45 UTC
    ...nothing is printed ...
    That might an indication that the top level $dsg_tags is undef .. you don't show us which variable you dumped.

            "Despite my privileged upbringing, I'm actually quite well-balanced. I have a chip on both shoulders."         - John Nash

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

    Please have a look at Simpler than XML::Simple. The data structures produced by XML::Simple are not consistent!

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

Re: Problem in reading parsed xml using XML::Simple
by u65 (Chaplain) on Jun 24, 2015 at 11:04 UTC

    Just wondering, are you on a Windows host?

    update: That was a stupid question--it's too early and I'm half asleep--pardon the noise!

    Update: Please Mr. Nodereaper, reap this node!