Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

reading multiple xmls in a folder and to get tagvalues stored and then printed to HTML

by numberuno (Initiate)
on Apr 26, 2012 at 19:57 UTC ( #967445=perlquestion: print w/ replies, xml ) Need Help??
numberuno has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, I need help here..reading multiple xmls in a folder and to get tagvalues stored and then printed to HTML Here is my code:
#!/usr/lib/perl use strict; use warnings; use Carp; use File::Find; use File::Spec::Functions qw( canonpath ); my $date = `date`; if ( @ARGV == 0 ) { push @ARGV, "C:\\Users\\bijoymeethal\\Desktop\\xml test"; warn "Using default path $ARGV[0]\n Usage: $0 path ...\n"; } open( my $allxml, '>', "all_xml_contents.xml" ) or die "can't open output xml file for writing: $!\n"; print $allxml '<?xml version="1.0" encoding="UTF-8"?>', "\n"; find( sub { return unless ( /[.]log\z/i and -f ); extract_information(); return; }, @ARGV ); sub extract_information { my $path = $_; if ( open my $xmlin, '<', $path ) { local $_ = <$xmlin>; print $allxml $_ unless (( /<\?xml/) or ( /<\testResults/)) ; #print $allxml $_ unless ( /<\testResults/); while ( <$xmlin> ) { print $allxml $_; } } return; }
here are my set of XMLS
#1 <?xml version="1.0" encoding="UTF-8"?> <testResults version="1.2"> <httpSample t="61964" lt="61964" ts="1334780908406" s="false" lb="/log +in.jsp" rc="504" rm="Gateway Time-out" tn="Thread Group 1-1" dt="text +" by="343"> <assertionResult> <name>Response Assertion</name> <failure>true</failure> <error>false</error> <failureMessage>Test failed: text expected to contain /Login/</fai +lureMessage> </assertionResult> </httpSample> <httpSample t="60017" lt="60017" ts="1334780970474" s="false" lb="buil +d info" rc="504" rm="Gateway Time-out" tn="Thread Group 1-1" dt="text +" by="343"> <assertionResult> <name>Response Assertion</name> <failure>true</failure> <error>false</error> <failureMessage>Test failed: code expected to contain /200/</failu +reMessage> </assertionResult> </httpSample> <httpSample t="60024" lt="60024" ts="1334781030567" s="false" lb="auth +enticate" rc="504" rm="Gateway Time-out" tn="Thread Group 1-1" dt="te +xt" by="343"> <assertionResult> <name>Response Assertion</name> <failure>true</failure> <error>false</error> <failureMessage>Test failed: text expected to contain /true/</fail +ureMessage> </assertionResult> </httpSample> <httpSample t="60003" lt="60003" ts="1334781090604" s="false" lb="data + usage" rc="504" rm="Gateway Time-out" tn="Thread Group 1-1" dt="text +" by="343"> <assertionResult> <name>Response Assertion</name> <failure>true</failure> <error>false</error> <failureMessage>Test failed: text expected to contain /true/</fail +ureMessage> </assertionResult> </httpSample> <httpSample t="60055" lt="60055" ts="1334781150611" s="false" lb="list + projects" rc="504" rm="Gateway Time-out" tn="Thread Group 1-1" dt="t +ext" by="343"> <assertionResult> <name>Response Assertion</name> <failure>true</failure> <error>false</error> <failureMessage>Test failed: text expected to contain /true/</fail +ureMessage> </assertionResult> </httpSample> <httpSample t="60012" lt="60012" ts="1334781210682" s="false" lb="Audi +t " rc="504" rm="Gateway Time-out" tn="Thread Group 1-1" dt="text" by +="343"> <assertionResult> <name>Response Assertion</name> <failure>true</failure> <error>false</error> <failureMessage>Test failed: text expected to contain /true/</fail +ureMessage> </assertionResult> </httpSample> <httpSample t="60023" lt="60022" ts="1334781270699" s="false" lb="Sear +ch Analytics default " rc="504" rm="Gateway Time-out" tn="Thread Grou +p 1-1" dt="text" by="343"> <assertionResult> <name>Response Assertion</name> <failure>true</failure> <error>false</error> <failureMessage>Test failed: text expected to contain /true/</fail +ureMessage> </assertionResult> </httpSample> <httpSample t="60012" lt="60012" ts="1334781330726" s="false" lb="data + source list projects" rc="504" rm="Gateway Time-out" tn="Thread Grou +p 1-1" dt="text" by="343"> <assertionResult> <name>Response Assertion</name> <failure>true</failure> <error>false</error> <failureMessage>Test failed: text expected to contain /true/</fail +ureMessage> </assertionResult> </httpSample> <httpSample t="60009" lt="60009" ts="1334781390743" s="false" lb="drag + and drop and see portlet chart " rc="504" rm="Gateway Time-out" tn=" +Thread Group 1-1" dt="text" by="343"> <assertionResult> <name>Response Assertion</name> <failure>true</failure> <error>false</error> <failureMessage>Test failed: text expected to contain /true/</fail +ureMessage> </assertionResult> </httpSample> <httpSample t="60027" lt="60027" ts="1334781450763" s="false" lb="sear +ch analytics refresh" rc="504" rm="Gateway Time-out" tn="Thread Group + 1-1" dt="text" by="343"> <assertionResult> <name>Response Assertion</name> <failure>true</failure> <error>false</error> <failureMessage>Test failed: text expected to contain /true/</fail +ureMessage> </assertionResult> </httpSample> <httpSample t="60020" lt="60020" ts="1334781510798" s="false" lb="filt +er on source file name " rc="504" rm="Gateway Time-out" tn="Thread Gr +oup 1-1" dt="text" by="343"> <assertionResult> <name>Response Assertion</name> <failure>true</failure> <error>false</error> <failureMessage>Test failed: text expected to contain /true/</fail +ureMessage> </assertionResult> </httpSample> </testResults> <httpSample t="61548" lt="61548" ts="1334781510904" s="false" lb="/log +in.jsp" rc="504" rm="Gateway Time-out" tn="Thread Group 1-1" dt="text +" by="343"> <assertionResult> <name>Response Assertion</name> <failure>true</failure> <error>false</error> <failureMessage>Test failed: text expected to contain /Login/</fai +lureMessage> </assertionResult> </httpSample> <httpSample t="60054" lt="60054" ts="1334781572583" s="false" lb="buil +d info" rc="504" rm="Gateway Time-out" tn="Thread Group 1-1" dt="text +" by="343"> <assertionResult> <name>Response Assertion</name> <failure>true</failure> <error>false</error> <failureMessage>Test failed: code expected to contain /200/</failu +reMessage> </assertionResult> </httpSample> <httpSample t="60010" lt="60009" ts="1334781632646" s="false" lb="auth +enticate" rc="504" rm="Gateway Time-out" tn="Thread Group 1-1" dt="te +xt" by="343"> <assertionResult> <name>Response Assertion</name> <failure>true</failure> <error>false</error> <failureMessage>Test failed: text expected to contain /true/</fail +ureMessage> </assertionResult> </httpSample> <httpSample t="60042" lt="60042" ts="1334781692732" s="false" lb="data + usage" rc="504" rm="Gateway Time-out" tn="Thread Group 1-1" dt="text +" by="343"> <assertionResult> <name>Response Assertion</name> <failure>true</failure> <error>false</error> <failureMessage>Test failed: text expected to contain /true/</fail +ureMessage> </assertionResult> </httpSample> <httpSample t="60092" lt="60092" ts="1334781752785" s="false" lb="list + projects" rc="504" rm="Gateway Time-out" tn="Thread Group 1-1" dt="t +ext" by="343"> <assertionResult> <name>Response Assertion</name> <failure>true</failure> <error>false</error> <failureMessage>Test failed: text expected to contain /true/</fail +ureMessage> </assertionResult> </httpSample> <httpSample t="60038" lt="60038" ts="1334781812884" s="false" lb="Audi +t " rc="504" rm="Gateway Time-out" tn="Thread Group 1-1" dt="text" by +="343"> <assertionResult> <name>Response Assertion</name> <failure>true</failure> <error>false</error> <failureMessage>Test failed: text expected to contain /true/</fail +ureMessage> </assertionResult> </httpSample> <httpSample t="60005" lt="60005" ts="1334781872928" s="false" lb="Sear +ch Analytics default " rc="504" rm="Gateway Time-out" tn="Thread Grou +p 1-1" dt="text" by="343"> <assertionResult> <name>Response Assertion</name> <failure>true</failure> <error>false</error> <failureMessage>Test failed: text expected to contain /true/</fail +ureMessage> </assertionResult> </httpSample> <httpSample t="60006" lt="60006" ts="1334781932938" s="false" lb="data + source list projects" rc="504" rm="Gateway Time-out" tn="Thread Grou +p 1-1" dt="text" by="343"> <assertionResult> <name>Response Assertion</name> <failure>true</failure> <error>false</error> <failureMessage>Test failed: text expected to contain /true/</fail +ureMessage> </assertionResult> </httpSample> <httpSample t="60014" lt="60014" ts="1334781992951" s="false" lb="drag + and drop and see portlet chart " rc="504" rm="Gateway Time-out" tn=" +Thread Group 1-1" dt="text" by="343"> <assertionResult> <name>Response Assertion</name> <failure>true</failure> <error>false</error> <failureMessage>Test failed: text expected to contain /true/</fail +ureMessage> </assertionResult> </httpSample> <httpSample t="60166" lt="60166" ts="1334782053031" s="false" lb="sear +ch analytics refresh" rc="504" rm="Gateway Time-out" tn="Thread Group + 1-1" dt="text" by="343"> <assertionResult> <name>Response Assertion</name> <failure>true</failure> <error>false</error> <failureMessage>Test failed: text expected to contain /true/</fail +ureMessage> </assertionResult> </httpSample> <httpSample t="60009" lt="60009" ts="1334782113216" s="false" lb="filt +er on source file name " rc="504" rm="Gateway Time-out" tn="Thread Gr +oup 1-1" dt="text" by="343"> <assertionResult> <name>Response Assertion</name> <failure>true</failure> <error>false</error> <failureMessage>Test failed: text expected to contain /true/</fail +ureMessage> </assertionResult> </httpSample> </testResults>
there are 6 similar xmls and what I need is to print only <failure>true</failure> <error>false</error> <failureMessage>Test failed: text expected to contain /Login/</failureMessage> to be printed into HTML

Comment on reading multiple xmls in a folder and to get tagvalues stored and then printed to HTML
Select or Download Code
Re: reading multiple xmls in a folder and to get tagvalues stored and then printed to HTML
by Kenosis (Priest) on Apr 26, 2012 at 22:40 UTC
      There is no example on how to read multiple XMLs and parse to HTML. The page just says "You could place this in a script to check multiple files in your XML folders." :(

        You have 3 tasks:

        1. read multiple files into memory
        2. parse those files via an XML parser
        3. do something with the parsed content (convert specific tag values to HTML
        XML::Simple merely accomplishes the second task. I recommend looking at Config::Any which will take care of tasks one AND two and you might not even need to do any heavy lifting to take care of the third task, but if you do there is always HTML::Template or Template::Toolkit.


        jeffa

        L-LL-L--L-LL-L--L-LL-L--
        -R--R-RR-R--R-RR-R--R-RR
        B--B--B--B--B--B--B--B--
        H---H---H---H---H---H---
        (the triplet paradiddle with high-hat)
        
Re: reading multiple xmls in a folder and to get tagvalues stored and then printed to HTML
by Kenosis (Priest) on Apr 27, 2012 at 07:02 UTC

    The following works with your xml (you have two </testResults> tags in your xml; there should be only one):

    use XML::Simple qw(:strict); my $xmlFile = 'textResults.xml'; my $xmlDocument = XMLin($xmlFile, ForceArray => 1, KeyAttr => {}, ); foreach my $httpSample (@{$xmlDocument->{httpSample}}) { print qq|<failure>$httpSample->{assertionResult}->[0]->{failure}-> +[0]</failure>\n|; print qq|<error>$httpSample->{assertionResult}->[0]->{error}->[0]< +/error>\n|; print qq|<failureMessage>$httpSample->{assertionResult}->[0]->{fai +lureMessage}->[0]</failureMessage>\n\n|; }

    Running this generates the following:

    <failure>true</failure> <error>false</error> <failureMessage>Test failed: text expected to contain /Login/</failure +Message> <failure>true</failure> <error>false</error> <failureMessage>Test failed: code expected to contain /200/</failureMe +ssage> <failure>true</failure> <error>false</error> <failureMessage>Test failed: text expected to contain /true/</failureM +essage> ...

    Add the code to process each file.

      I could reach to this level with your help, but some silly mistake is haunting..
      use XML::Simple qw(:strict); use File::Find; use strict; use warnings; use Carp; use File::Find; use XML::Parser; use File::Spec::Functions qw( canonpath ); if ( @ARGV == 0 ) { push @ARGV, "D:\\xml test"; warn "Using default path $ARGV[0]\n Usage: $0 path ...\n"; } open(HTML_FILE, ">JBAT_Report.html") || die "Can't open file: $!\n"; # Print the initial HTML tags print HTML_FILE "<html>\n<body>\n<h1>BAT Report - JMeter Test</h1>\n<h +r><br><table class=MsoTableGrid border=1 cellspacing=0 cellpadding=0 +><tr><th><p>TestPlan</p></th><th><p>Test Failed?</p></th><th><p>Failu +reMessage</p></th><th><p>Link to Source</p></th></tr>"; find( sub { return unless ( /[.]log\z/i and -f ); #getting multiple xml fi +les with .log format extract_information(); return; }, @ARGV ); sub extract_information { my( $error, $value, $failure, $failureMessage ) = ("") x 4; my $xmlFile = $_; # is this correct? #print HTML_FILE $xmlFile; my $xmlDocument = XMLin($xmlFile, ForceArray => 1, KeyAttr => {}, ); # it shows error here foreach my $httpSample (@{$xmlDocument->{httpSample}}) { print HTML_FILE qq| $httpSample->{assertionResult}->[0]->{failure} +->[0]\n|; print HTML_FILE qq| $httpSample->{assertionResult}->[0]->{error}-> +[0]\n|; print HTML_FILE qq| $httpSample->{assertionResult}->[0]->{failureM +essage}->[0]\n\n|; } print HTML_FILE <<"EOF"; <tr><td><p>$value</p><td><p>$failure</p></td><td><p>$failureMessage</p +></td></tr> EOF return; } print HTML_FILE "</body><br></html>"; close (HTML_FILE);
      But its throwing "File does not exist: 0 at 98675.pl line 33"

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://967445]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2014-08-30 14:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (293 votes), past polls