tags:

views:

15

answers:

2

Hi All,

I have a xml file like this

<testcase>
<sf_params>
 <dir>1</dir>
 <sfid>2</sfid>
</sf_params>
</testcase>

I used xml::simple (perl parsing) and got the o/p as

$VAR1 = {
          'sf_params' => [
                         {
                           'sfid' => [
                                     '2'
                                   ],
                           'dir' => [
                                    '1'
                                  ]
                         }
                       ]
        };

How can i access or assign value of dir to a variable e.g. $dir = $var1->{sf_params}->{dir}

+1  A: 

Would XML::XPath module help you? Try code like

my $file = XML::XPath->new (xml => "/path/to/your/file.xml");
my $dirs = $file->find('/sf_params/dir');
foreach my $foo ($dirs->get_nodelist) {
     printf "dir is %s\n",$foo->string_value;
}

and adjust it to your needs :)

Janne Pikkarainen
+1  A: 

Don't use ForceArray => 1 option (= use ForceArray => 0). Then you will get

$VAR1 = {
          'sf_params' => {
                         'sfid' => '2',
                         'dir' => '1'
                       }
        };

and

$dir = $var1->{sf_params}->{dir}

will work. Or if you have to use ForceArray:

$dir = $var1->{sf_params}->[0]->{dir}->[0];

will work in that case, but it's ugly.

phadej
it is not working for me
Senthil kumar