lunchlady55 has asked for the wisdom of the Perl Monks concerning the following question:
I'm very lost. I am trying to do the following:
I have an XML file from Nagios. Here's the structure:
1 <?xml version="1.0" encoding="UTF-8" standalone="yes"?> 2 <NAGIOS> 3 <DATASOURCE> 4 <TEMPLATE>check_bigip_pool_connection</TEMPLATE> 5 <IS_MULTI>0</IS_MULTI> 6 <DS>1</DS> 7 <NAME>cur</NAME> 8 <UNIT></UNIT> 9 <ACT>2198</ACT> 10 <WARN></WARN> 11 <WARN_MIN></WARN_MIN> 12 <WARN_MAX></WARN_MAX> 13 <WARN_RANGE_TYPE></WARN_RANGE_TYPE> 14 <CRIT></CRIT> 15 <CRIT_MIN></CRIT_MIN> 16 <CRIT_MAX></CRIT_MAX> 17 <CRIT_RANGE_TYPE></CRIT_RANGE_TYPE> 18 <MIN></MIN> 19 <MAX></MAX> 20 </DATASOURCE> 21 <DATASOURCE> 22 <TEMPLATE>check_bigip_pool_connection</TEMPLATE> 23 <IS_MULTI>0</IS_MULTI> 24 <DS>2</DS> 25 <NAME>max</NAME> 26 <UNIT></UNIT> 27 <ACT>5400</ACT> 28 <WARN></WARN> 29 <WARN_MIN></WARN_MIN> 30 <WARN_MAX></WARN_MAX> 31 <WARN_RANGE_TYPE></WARN_RANGE_TYPE> 32 <CRIT></CRIT> 33 <CRIT_MIN></CRIT_MIN> 34 <CRIT_MAX></CRIT_MAX> 35 <CRIT_RANGE_TYPE></CRIT_RANGE_TYPE> 36 <MIN></MIN> 37 <MAX></MAX> 38 </DATASOURCE> ...
Much more follows. The above section is the part I care about. I want to get data from the <ACT> tags associated with the <NAME> "cur".
I'm reading it in with XML::Simple with the following code:
1 #!/usr/bin/env perl 2 3 4 use Data::Dumper; 5 use XML::Simple; ... 9 my $xs1 = XML::Simple->new(); 10 my $file = $ARGV[0]; 11 my $doc = $xs1->XMLin($file, forcearray => 1); 12
When I use Dumper($datasource), I get this:
$VAR1 = { 'NAGIOS_NOTIFICATIONRECIPIENTS' => {}, ... 'NAGIOS_HOSTACTIONURL' => '/nagios/pnp/index.php?host=ahostn +ame.somedomain.com', 'DATASOURCE' => [ { 'MIN' => {}, 'TEMPLATE' => 'check_bigip_pool_connection +', 'CRIT_MAX' => {}, 'MAX' => {}, 'UNIT' => {}, 'WARN_MAX' => {}, 'NAME' => 'cur', 'WARN_MIN' => {}, 'IS_MULTI' => '0', 'DS' => '1', 'WARN' => {}, 'CRIT_MIN' => {}, 'ACT' => '2198', 'CRIT' => {}, 'CRIT_RANGE_TYPE' => {}, 'WARN_RANGE_TYPE' => {} }, ...
Next, I ran this:
20 foreach my $datasource ($doc->{DATASOURCE}){ 21 22 print Dumper($datasource->[1]->{NAME}->[0]); 23 24 my $element = scalar(@datasource); 25 26 print "Element = $element\n"; 27 for(my $element=0; $element < scalar(@datasource); $element++){ 28 print $datasource[$element]; 29 } 30 }
and as far as I understand, datasource should be an array of hashes. But $element is 0, before I use it in the for loop. Output:
$VAR1 = 'max'; Element = 0
Now here's the weird part where I decided to ask for help. When I change the print statement to
22 print Dumper($datasource[1]->{NAME}->[0]);(notice the lack of '->' after $datasource) it changes my output to
$VAR1 = undef; Element = 2 HASH(0x10e2ef0)
Thank You.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Strange Behavior with XML::Simple when pulling info out of an XML file
by kcott (Archbishop) on Nov 27, 2010 at 00:20 UTC | |
|
Re: Strange Behavior with XML::Simple when pulling info out of an XML file
by roboticus (Chancellor) on Nov 26, 2010 at 23:16 UTC | |
by lunchlady55 (Initiate) on Nov 27, 2010 at 00:28 UTC | |
by Marshall (Canon) on Nov 27, 2010 at 13:48 UTC | |
|
Re: Strange Behavior with XML::Simple when pulling info out of an XML file
by Jenda (Abbot) on Nov 27, 2010 at 23:11 UTC | |
by lunchlady55 (Initiate) on Nov 29, 2010 at 21:14 UTC |