Readig XML file from Perl -


i have xml file contains "squishreport". want special value of nodes under condition. so, have written code in perl main function follows:

sub filecount { chdir( $dir ) or die "couldn't go inside $dir directory, $!"; opendir(my $dh, $dir) or die "$0: $dir: $!\n"; while (my $file = readdir($dh)) {     # want files      next unless (-f "$dir/$file");     # use regular expression find files ending in .xml      next unless ($file =~ m/$suff$/);       $xml = xml::libxml->load_xml(location => $file);      # iterate entries      $test ($xml->findnodes('/squishreport/test/test')) {         $name_test = $test->findvalue('@name');         # trim value          s/\a\s*//, s/\s*\z// $value_test = $test->textcontent;          print "test name = $name_test \n";          $result ($test->findnodes('/verification/result')) {             $type_result = $result->findvalue('@type');              # trim value              s/\a\s*//, s/\s*\z// $value = $result->textcontent;              print "$type_result = $value \n";             if (($type_result eq "warning") || ($type_result eq "fail") || ($type_result eq "error") || ($type_result eq "fatal")){                 print "$type_result = $value \n";                 print "***********  $name_test not ok *********** \n\n ";              }             else{                 print "***********  $name_test ok *********** \n\n ";             }         }         print "\n";     }     #}      print "$file\n";             $count = $count + 1; } closedir($dh); if ($count eq 1){     print "there 1 file in '' $dir '' directory."; } else{     print "there $count files in '' $dir '' directory."; }    

}

the goal looking first test , type name. if there pass in type write name of test , says ok, otherwise writes not ok.

i not sure if code right code structure/goal.

my problem inner for loop not work. instruction xml file as:

<squishreport>      <test name="abcd">          <test name="efgh">              <verification .......>                   <result type="pass">                          <description>version check test case passes!</description>                          <description type="detailed"></description>     .     .     .                   <test name=ijklm">              <verification .......>                   <result type="pass">                          <description>version check test case passes!</description>                          <description type="detailed"></description> 

**edit: ** here xml file:

<squishreport> <test name="mat_tst_versions">     <prolog time="2015-06-16t13:49:54+02:00"/>     <message type="fatal" time="2015-06-16t13:50:54+02:00">         <description><![cdata[execution error]]></description>         <description type="detailed"><![cdata[squishrunner exited code 3 when executing tst_start_app]]></description>     </message>     <test name="tst_check_versions">         <prolog time="2015-06-16t13:50:55+02:00"/>         <message line="274" type="log" file="c:\test\scripts\utilities.py" time="2015-06-16t13:50:59+02:00">             <description><![cdata[is 64 bit windows version: true]]></description>         </message>                     <message line="124" type="log" file="c:\test\mat_tst\tst_check_versions\test.py" time="2015-06-16t13:50:59+02:00">             <description><![cdata[    path exists!]]></description>         </message>         <message line="129" type="fail" file="c:\test\mat_tst\tst_check_versions\test.py" time="2015-06-16t13:50:59+02:00">             <description><![cdata[expected version: 1.0.0.107]]></description>         </message>         <verification line="132" type="" name="" file="c:\test\mat_tst\tst_check_versions\test.py">             <result type="pass" time="2015-06-16t13:50:59+02:00">                 <description>version check test case passes!</description>                 <description type="detailed"></description>             </result>         </verification>         <epilog time="2015-06-16t13:50:59+02:00"/>     </test>             <test name="tst_scheduler_1">         <prolog time="2015-06-16t13:51:00+02:00"/>         <message line="62" type="log" file="c:\test\mat_tst\tst_scheduler_1\test.py" time="2015-06-16t13:51:00+02:00">             <description><![cdata[count = 0]]></description>         </message>         <message line="125" type="log" file="c:\test\scripts\start_stop.py" time="2015-06-16t13:51:06+02:00">             <description><![cdata[duration of start of c: 5132ms]]></description>         </message>         <message line="172" type="log" file="c:\test\mat_tst\tst_scheduler_1\test.py" time="2015-06-16t13:51:19+02:00">             <description><![cdata[application name = c]]></description>         </message>         <message line="184" type="log" file="c:\test\mat_tst\tst_scheduler_1\test.py" time="2015-06-16t13:51:19+02:00">             <description><![cdata[start application: c]]></description>         </message>         <message line="213" type="log" file="c:\test\mat_tst\tst_scheduler_1\test.py" time="2015-06-16t13:51:19+02:00">             <description><![cdata[scheduler label exists]]></description>         </message>         <message line="219" type="log" file="c:\test\mat_tst\tst_scheduler_1\test.py" time="2015-06-16t13:51:19+02:00">             <description><![cdata[scheduler counter exists]]></description>         </message>         <verification line="368" type="" name="" file="c:\test\mat_tst\tst_scheduler_1\test.py">             <result type="fail" time="2015-06-16t13:52:01+02:00">                 <description>different types elements</description>                 <description type="detailed"></description>             </result>         </verification>         <verification line="369" type="" name="" file="c:\test\mat_tst\tst_scheduler_1\test.py">             <result type="fail" time="2015-06-16t13:52:01+02:00">                 <description>name_list= ['scheduler', 'ncu', 'plcsimadv', 'simnck', 'mcp']</description>                 <description type="detailed"></description>             </result>         </verification>         <verification line="370" type="" name="" file="c:\test\mat_tst\tst_scheduler_1\test.py">             <result type="fail" time="2015-06-16t13:52:01+02:00">                 <description>typ_list= ['scheduler', u'ncu', u'plcsimadv']</description>                 <description type="detailed"></description>             </result>         </verification>         <message line="393" type="log" file="c:\test\mat_tst\tst_scheduler_1\test.py" time="2015-06-16t13:52:08+02:00">             <description><![cdata[:840evovc.lblclockcounter_edit = 0]]></description>         </message>         <verification line="411" type="" name="" file="c:\test\mat_tst\tst_scheduler_1\test.py">             <result type="fail" time="2015-06-16t13:52:08+02:00">                 <description>scheduler counter 1. read, n=0: scheduler= 0</description>                 <description type="detailed"></description>             </result>         </verification>         <verification line="414" type="" name="" file="c:\test\mat_tst\tst_scheduler_1\test.py">             <result type="fail" time="2015-06-16t13:52:08+02:00">                 <description>scheduler counter 2. read, n=0: scheduler= 0</description>                 <description type="detailed"></description>             </result>         </verification>         <message line="421" type="log" file="c:\test\mat_tst\tst_scheduler_1\test.py" time="2015-06-16t13:52:08+02:00">             <description><![cdata[scheduler counter 1. read < counter 2. read, n=0: scheduler= 0]]></description>         </message>         <message line="484" type="error" file="c:\test\mat_tst\tst_scheduler_1\test.py" time="2015-06-16t13:52:29+02:00">             <description><![cdata[script error]]></description>             <description type="detailed"><![cdata[lookuperror: object ':840evovc.chkschedule_checkbox_2' not ready.]]></description>         </message>         <epilog time="2015-06-16t13:52:29+02:00"/>     </test>     <test name="tst_run_plc">         <prolog time="2015-06-16t13:52:30+02:00"/>         <verification line="45" type="" name="" file="c:\test\mat_tst\tst_run_plc\test.py">             <result type="fail" time="2015-06-16t13:52:30+02:00">                 <description>user path folder exists!</description>                 <description type="detailed"></description>             </result>         </verification>         <message line="125" type="log" file="c:\test\scripts\start_stop.py" time="2015-06-16t13:52:34+02:00">             <description><![cdata[duration of start of 840evovc: 4263ms]]></description>         </message>         <message line="54" type="log" file="c:\test\mat_tst_840evovc\tst_run_plc\test.py" time="2015-06-16t13:52:34+02:00">             <description><![cdata[start ok, duration = 4263000]]></description>         </message>         <message line="64" type="log" file="c:\test\mat_tst_840evovc\tst_run_plc\test.py" time="2015-06-16t13:52:34+02:00">             <description><![cdata[application started: 840evovc]]></description>         </message>         <message line="78" type="log" file="c:\test\mat_tst_840evovc\tst_run_plc\test.py" time="2015-06-16t13:52:39+02:00">             <description><![cdata[plc sim advanced tmp user files expected files]]></description>         </message>         <message line="263" type="log" file="c:\test\scripts\start_stop.py" time="2015-06-16t13:52:45+02:00">             <description><![cdata[all applications closed]]></description>         </message>         <message line="87" type="log" file="c:\test\mat_tst_840evovc\tst_run_plc\test.py" time="2015-06-16t13:52:45+02:00">             <description><![cdata[close application ok]]></description>         </message>         <epilog time="2015-06-16t13:52:45+02:00"/>     </test>     <test name="tst_app_resources">         <prolog time="2015-06-16t13:52:47+02:00"/>         <message line="65" type="log" file="c:\test\mat_tst\tst_app_resources\test.py" time="2015-06-16t13:52:49+02:00">             <description><![cdata[count:0]]></description>         </message>         <message line="125" type="log" file="c:\test\scripts\start_stop.py" time="2015-06-16t13:52:53+02:00">             <description><![cdata[duration of start: 4176ms]]></description>         </message>         <message line="73" type="log" file="c:\test\mat_tst\tst_app_resources\test.py" time="2015-06-16t13:52:53+02:00">             <description><![cdata[start ok, duration = 4176000]]></description>         </message>         <message line="43" type="log" file="c:\test\scripts\win32utilities.py" time="2015-06-16t13:53:00+02:00">             <description><![cdata[application 840evovc exists once]]></description>         </message>                     <message line="138" type="error" file="c:\test\mat_tst_\tst_app_resources\test.py" time="2015-06-16t13:53:30+02:00">             <description><![cdata[script error]]></description>             <description type="detailed"><![cdata[lookuperror: object ':chkschedule_checkbox_2' not ready.                 called from:             c:\test\mat_tst\tst_app_resources\test.py: 82]]></description>         </message>         <epilog time="2015-06-16t13:53:30+02:00"/>     </test>     <test name="tst_reset">         <prolog time="2015-06-16t13:53:32+02:00"/>            <message line="260" type="log" file="c:\test\scripts\start_stop.py" time="2015-06-16t13:56:12+02:00">             <description><![cdata[process exited.]]></description>                   </message>         <message line="263" type="log" file="c:\test\scripts\start_stop.py" time="2015-06-16t13:56:12+02:00">             <description><![cdata[all applications closed]]></description>         </message>         <message line="284" type="log" file="c:\test\mat_tst\tst_reset\test.py" time="2015-06-16t13:56:12+02:00">             <description><![cdata[application closed]]></description>         </message>         <verification line="287" type="" name="" file="c:\test\mat_tst\tst_reset\test.py">             <result type="pass" time="2015-06-16t13:56:12+02:00">                 <description>tst_reset_1 ok</description>                 <description type="detailed"></description>             </result>         </verification>         <epilog time="2015-06-16t13:56:12+02:00"/>     </test>     <epilog time="2015-06-16t13:56:13+02:00"/> </test> 

this won't help:

  • if ($count eq 1) - should ==
  • rather readdir filtering, why not use glob ( "$dir/*.xml" )?
  • my $xml = xml::libxml->load_xml(location => $file); breaking, because you're not changing directory. (so location should $dir/$file - problem using above glob avoids)

however core of problem this:

for $result ( $test->findnodes('/verification/result') ) { 

change to:

for $result ( $test->findnodes('./verification/result') ) { 

and you'll get:

test name = tst_app_resources   test name = tst_reset  ***********  tst_reset ok ***********  

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -