从perl 中读取xml文件,求助!!!!!!!!!!!!!!!!!!

yanheping 发布于 2011/10/27 15:39
阅读 1K+
收藏 0

<?xml version="1.0" encoding="UTF-8"?>
<root>
 <row>
  <field name='CURRENT_DATE'  value='2011-10-25'/>
  <field name='GATEWAY_NAME'  value='HESJ-PS-WAP-SYS01'/>
  <field name='REQ_NUM'  value='119339386'/>
  <field name='TODAY_SUCC_RATE'  value='95.95'/>
  <field name='TODAY_TOP_LINE_USER_NUM'  value='412'/>
  <field name='GATEWAY_AVG_DELAY'  value='33.03'/>
 </row>
 <row>
  <field name='CURRENT_DATE'  value='2011-10-26'/>
  <field name='GATEWAY_NAME'  value='HESJ-PS-WAP-SYS01'/>
  <field name='REQ_NUM'  value='3318745'/>
  <field name='TODAY_SUCC_RATE'  value='95'/>
  <field name='TODAY_TOP_LINE_USER_NUM'  value='127'/>
  <field name='GATEWAY_AVG_DELAY'  value='37.56'/>
 </row>

</root>

如何用perl 从上面的xml 文件中读取数据?

加载中
0
y
yanheping
没人知道吗?
0
樊睿
樊睿

open(FH, "XMLfilename");

while(<FH>)

{

    #do something ,the data in $_ variable;

}

close(FH);

0
linuxwt
linuxwt
open my ($fh), '<', $xmlfilename or die;
    while (<$fh>) 
{
#对当前行$_做相关处理
}
close(fh);

0
sjdy521
sjdy521
有专业的模块:XML::Simple
0
cystcOS
cystcOS

楼上说的对,直接API

Dumper 看下结构 一个个拿出来就行了

my $xmlfile = dirname($0) . "\\testaaa.xml";

    my $xmlcontent = XMLin($xmlfile);

    print Dumper($xmlcontent);

    

    my @lines = @{$xmlcontent->{Baselines}->{Baseline}};

    my $a  = @lines;

    print($a);

    foreach $bases (@lines)

    {

        print($bases->{deliverer}.":");

        print($bases->{content}."\n");

        

        if($bases->{deliverer} eq "NPF")

        {

        	print($bases->{content}."\n");

        }

    }

 

0
cups_book
cups_book

引用来自“cystcOS”的答案

楼上说的对,直接API

Dumper 看下结构 一个个拿出来就行了

my $xmlfile = dirname($0) . "\\testaaa.xml";

    my $xmlcontent = XMLin($xmlfile);

    print Dumper($xmlcontent);

    

    my @lines = @{$xmlcontent->{Baselines}->{Baseline}};

    my $a  = @lines;

    print($a);

    foreach $bases (@lines)

    {

        print($bases->{deliverer}.":");

        print($bases->{content}."\n");

        

        if($bases->{deliverer} eq "NPF")

        {

        	print($bases->{content}."\n");

        }

    }

 

有一行报错:

Can't use an undefined value as an ARRAY reference

第九行:

my @lines = @{$xmlcontent->{Baselines}->{Baseline}};

0
cups_book
cups_book

本地已经有这两个package了。

返回顶部
顶部