When faced by these sort of problems it is a good practice to dump out the data structure you think you are dealing with, I use YAML, others swear by Data::Dumper. Which ever you choose it will help.

One problem is that $xml doesn't have a key called tc, it has a key called dut, dumping out $xml would have shown that. The other is that you want to use result as a key but XML::Simple will use the value of the first attribute as a key. This is some code that works on the example provided. I don't know if it will work on the full set of data.

use XML::Simple; my $XML_string=<<'XML'; <exec> <dut> <tc id="001.001" result="Passed"> <ts/> </tc> <tc id="002.001" result="Failed"> <ts/> </tc> <tc id="003.001" result="Warnings"> <ts/> </tc> </dut> </exec> XML # create object my $xmlfile = XML::Simple->new(KeyAttr => {result => 'result'} ); my $xml = $xmlfile->XMLin($XML_string); my $Passed = 0; my $Warnings = 0; my $Failed = 0; print Dump $XML; foreach my $TCresult (@{$xml->{dut}{tc}}){ print Dump $TCresult; if ($TCresult->{result} eq 'Passed'){$Passed++;} if ($TCresult->{result} eq 'Warnings'){$Warnings++;} if ($TCresult->{result} eq 'Failed'){$Failed++;} } print "Passed: $Passed\n"; print "Warnings: $Warnings\n"; print "Failed: $Failed\n";

Example YAML output

--- dut: tc: - id: 001.001 result: Passed ts: {} - id: 002.001 result: Failed ts: {} - id: 003.001 result: Warnings ts: {}
Example Data::Dumper output (indent set to 4 spaces)
$VAR1 = [ { 'dut' => { 'tc' => { '002.001' => { 'ts' => {}, 'result' => 'Failed' }, '001.001' => { 'ts' => {}, 'result' => 'Passed' }, '003.001' => { 'ts' => {}, 'result' => 'Warnings' } } } } ];


In reply to Re: XML::Simple parse attributes by hipowls
in thread XML::Simple parse attributes by hakana

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.