perl - Trouble in reading values from parsed XML data using XML::Simple -


i half way through writing script using xml::simple. have read not "simple", , own documentation discourages use in new code, have no other choice script extension existing code.

what doing this

  1. get xml reading url
  2. parse using xml::simple
  3. read required elements data
  4. run different checks on these required elements

i parse , checks on few of elements, while reading elements in array, getting undef.

this code:

#!/usr/bin/perl  use strict; use warnings;  use lwp::useragent; use lwp::simple; use xml::simple; use dbi;  use data::dumper;  $str = "<actual_url>";  $ua = lwp::useragent->new; $ua->timeout( 180 ); $ua->agent( "$0/0.1 " . $ua->agent );  $req = http::request->new( => $str );  $buffer; $req->content_type( 'text/xml' ); $req->content( $buffer );  $response = $ua->request( $req );  $xml = $response->content(); print "value of \$xml is:\n"; print $xml;  $filename = 'record.txt'; open( $fh, '>', $filename ) or die "could not open file '$filename' $!"; print $fh $xml; close $fh;  $number_of_lines = `wc -l record.txt | cut -d' ' -f1`; print "number of lines in $filename are: $number_of_lines\n"; if ( $number_of_lines >= 50 ) {     print "test_1 success\n"; }  $mysql_dbh; $test_id;  $xst; %cmts_pre_eq_tags;  if ( ( not defined $xml ) or ( $xml =~ m/read\stimeout/i ) ) {     &printxmlerr( 'drum request timed out' ); } else {     $xs = xml::simple->new();     $xst = eval { $xs->xmlin( $xml, keyattr => 1 ) };     &printxmlerr( $@ ) if ( $@ );     print "value of \$xst inside is:\n";     print dumper( $xst ); }  $cmts_pre_eq_tags{'$cmts_pre_eq_groupdelaymag'} =     $xst->{cmts}->{pre_eq}->{groupdelaymag}->{content};  #more elements checked here $cmts_pre_eq_tags{'$cmts_pre_eq_icfr'} =     $xst->{cmts}->{pre_eq}->{icfr}->{content};  $decision1 = 1; print "\%cmts_pre_eq_tags:\n"; foreach ( sort keys %cmts_pre_eq_tags ) {     print "$_ : $cmts_pre_eq_tags{$_}\n";     if ( $cmts_pre_eq_tags{$_} eq '' ) {         print "$_ empty!\n";         $decision1 = 0;     } } print "\n";  if ( $decision1 == 0 ) {     print "test_2_1 fail\n"; } else {     print "test_2_1 success\n"; }  $cpeip4 = $xst->{cmts}->{cpeip4}->{content}; print "the cpe ip is: $cpeip4\n";  if ( $cpeip4 ne '' ) {     print "test_2_2 success\n"; } else {     print "test_2_2 fail\n"; }  # working fine until here, following 2 print showing undef print dumper ( $xst->{cmts}{stbdsg}{dsg}[0]{dsgifstdtunnelfiltertunnelid} ); print dumper ( $xst->{cmts}{stbdsg}{dsg}[0]{dsgifstdtunnelfilterclientidtype} ); print "after\n"; 

output of last 3 print statements is:

$var1 = undef; $var1 = undef; after 

i can't provide entire xml or output of print dumper($xst) it's big , gets generated dynamically, i'll provide sample of it.

the part of xml causing trouble is

<cmts>   <stbdsg>     <dsg>       <dsgifstdtunnelfiltertunnelid>1</dsgifstdtunnelfiltertunnelid>       <dsgifstdtunnelfilterclientidtype>casystemid</dsgifstdtunnelfilterclientidtype>     </dsg>     <dsg>       <dsgifstdtunnelfiltertunnelid>2</dsgifstdtunnelfiltertunnelid>       <dsgifstdtunnelfilterclientidtype>gasystemid</dsgifstdtunnelfilterclientidtype>     </dsg>   </stbdsg> </cmts> 

and when part parsed, corresponding output in $xst is

$var1 = {     'cmts' => {             'stbdsg' => {                 'dsg' => [                          {                            'dsgifstdtunnelfiltertunnelid' => '1',                            'dsgifstdtunnelfilterclientidtype' => 'casystemid',                          },                          {                            'dsgifstdtunnelfiltertunnelid' => '2',                            'dsgifstdtunnelfilterclientidtype' => 'gasystemid',                          }                          ]                      },     }, }; 

the xml part after parsing values fetched fine this

<cmts>     <name field_name="name">cts01nsocmo</name>     <object field_name="nemos object">888</object>     <vendor field_name="vendor">xyz</vendor> </cmts> 

which converted as:

    $var1 = {       'cmts' => {         'name' => {                     'content' => 'cts01nsocmo',                     'field_name' => 'name'                   },         'object' => {                       'content' => '888',                       'field_name' => 'nemos object'                     },         'vendor' => {                       'content' => 'xyz',                       'field_name' => 'vendor'                     }          }, }; 

so when there no array in parsed content, values being fetched correctly in variables.

it seems reason why this

print dumper ( $xst->{cmts}{stbdsg}{dsg}[0]{dsgifstdtunnelfiltertunnelid} ); print dumper ( $xst->{cmts}{stbdsg}{dsg}[0]{dsgifstdtunnelfilterclientidtype} ); 

is getting undef related setting correct values either keyattr or forcearray. trying find reading xml::simple, wanted see if there's distinct missing here.

it's worth considering use of xml::twig, regardless of rest of project does

in particular, xml::twig::elt objects -- module's implementation of xml elements -- have simplify method, documentation says this

return data structure suspiciously similar xml::simple's. options identical xmlin options

so can use xml::twig precision , convenience, , apply simplify method if need pass on data looks xml::simple data structure


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -