in reply to XML::Simple parse attributes
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
Example Data::Dumper output (indent set to 4 spaces)--- dut: tc: - id: 001.001 result: Passed ts: {} - id: 002.001 result: Failed ts: {} - id: 003.001 result: Warnings ts: {}
$VAR1 = [ { 'dut' => { 'tc' => { '002.001' => { 'ts' => {}, 'result' => 'Failed' }, '001.001' => { 'ts' => {}, 'result' => 'Passed' }, '003.001' => { 'ts' => {}, 'result' => 'Warnings' } } } } ];
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: XML::Simple parse attributes
by hakana (Acolyte) on Jan 29, 2008 at 12:44 UTC |