1

I am new to perl and trying to parse XML using XML:Simple

My xml is

<suite suiteId="45" instanceId="3485">
   <project>Test project</project>
   <testcase id="2346" name="abc" suite="TEst1" priority="1" severity="1" owner="domain" category="BAT" timeout="10">
    <description>Checking Test1</description>
    <testExecTimeInMins>2</testExecTimeInMins>
    <status>Failed</status>
    <testServer id="86" name="host1" ip="1.2.3.4" platform="Linux" database="MySQL" buildNo="" />
    <error></error>
  </testcase>
  <testcase id="2346456" name="abc123" suite="TEst2" priority="1" severity="1" owner="domain" category="BAT" timeout="10">
    <description>Checking Test2</description>
    <testExecTimeInMins>6</testExecTimeInMins>
    <status>Passed</status>
    <testServer id="86" name="host1" ip="1.2.3.4" platform="Linux" database="MySQL" buildNo="" />
    <error />
  </testcase>
 </suite>

How do I get the values of testcase id, name, suite? How to get the values for testServer, id, name?

I tried to access it as shown below but it throws "Not an Array reference at"

$XMLData = XMLin($targetFile);


foreach my $testcases (@{$XMLData->{testcase}}){
    $logger->info("$testcases->{id}");
}

1 Answer 1

1

If you examine the actual data that is coming out of XMLin, you would see that you don't have an array reference (just like the error message states), but instead have a hashref that is keyed on the name of the testcase:

use strict;
use warnings; 

use Data::Dumper; 
use XML::Simple; 

my $xml = XMLin("/Users/mcmillhj/temp.xml");

print Dumper $xml;

__DATA__
{
          'instanceId' => '3485',
          'project' => 'Test project',
          'testcase' => {
                        'abc123' => {
                                    'owner' => 'domain',
                                    'priority' => '1',
                                    'status' => 'Passed',
                                    'suite' => 'TEst2',
                                    'testExecTimeInMins' => '6',
                                    'description' => 'Checking Test2',
                                    'timeout' => '10',
                                    'error' => {},
                                    'category' => 'BAT',
                                    'id' => '2346456',
                                    'severity' => '1',
                                    'testServer' => {
                                                    'database' => 'MySQL',
                                                    'buildNo' => '',
                                                    'ip' => '1.2.3.4',
                                                    'name' => 'host1',
                                                    'platform' => 'Linux',
                                                    'id' => '86'
                                                  }
                                  },
                        'abc' => {
                                 'owner' => 'domain',
                                 'priority' => '1',
                                 'status' => 'Failed',
                                 'suite' => 'TEst1',
                                 'testExecTimeInMins' => '2',
                                 'description' => 'Checking Test1',
                                 'timeout' => '10',
                                 'error' => {},
                                 'category' => 'BAT',
                                 'id' => '2346',
                                 'severity' => '1',
                                 'testServer' => {
                                                 'database' => 'MySQL',
                                                 'buildNo' => '',
                                                 'ip' => '1.2.3.4',
                                                 'name' => 'host1',
                                                 'platform' => 'Linux',
                                                 'id' => '86'
                                               }
                               }
                      },
          'suiteId' => '45'
        };

To get all testcases, you just need to iterate over the keys:

foreach my $testcasename ( keys %{ $XMLData->{testcase} } ){
    $logger->info($XMLdata->{testcase}->{$testcasename}->{id});
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.