开发者

perl xml parsing help

I have a xml file like this

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

I used x开发者_JAVA技巧ml::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}


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 :)


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜