如何使用Perl的XML :: SAX解析文件?

时间:2010-09-17 09:35:56

标签: xml perl sax

我正在尝试XML::SAX解析器来解析XML文件。 XML文件如下所示,

            <message>
                <c1>
                    <rrcConnectionSetupComplete>
                        <rrc-TransactionIdentifier>2</rrc-TransactionIdentifier>
                        <criticalExtensions>
                            <c1>
                                <rrcConnectionSetupComplete-r8>
                                    <selectedPLMN-Identity> 1 </selectedPLMN-Identity>
                                    <dedicatedInfoNAS> 07410109014290112345671000028020000f0 </dedicatedInfoNAS>
                                </rrcConnectionSetupComplete-r8>
                            </c1>
                        </criticalExtensions>
                    </rrcConnectionSetupComplete>
                </c1>
            </message>

Perl代码如下所示,

use strict;

use XML::SAX;
use MySAXHandler;

my $parser = XML::SAX::ParserFactory->parser(Handler => MySAXHandler->new);

$parser->parse_uri("uL-DCCH-Message.xml");

my $rrc_trans_identifier = $parser->{'c1'}->{'rrcConnectionSetupComplete'}->{'rrc-TransactionIdentifier'};
print "rrc_trans_id :: $rrc_trans_identifier\n";

my $selected_plmn_id = $parser->{c1}->{rrcConnectionSetupComplete}->{criticalExtensions}->{c1}->{'rrcConnectionSetupComplete-r8'}->{'selectedPLMN-Identity'};
print "plmn identity :: $selected_plmn_id\n";

my $rrc_dedicated_info_nas = $parser->{c1}->{rrcConnectionSetupComplete}->{criticalExtensions}->{c1}->{'rrcConnectionSetupComplete-r8'}->{dedicatedInfoNAS};
print "dedicated info nas :: $rrc_dedicated_info_nas\n";

当我运行此代码时,我输出为

Can't locate MySAXHandler.pm in @INC (@INC contains: /etc/perl /usr/local/lib/perl/5.10.0 /usr/local/share/perl/5.10.0 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.10 /usr/share/perl/5.10 /usr/local/lib/site_perl .) at sax.pl line 4.
BEGIN failed--compilation aborted at sax.pl line 4.

我从CPAN安装了XML :: SAX,但它仍然显示了一些模块缺失错误。

我的问题是,

  1. 是否需要安装其他模块?

  2. 要访问XML文件中的值,我所遵循的程序是否正确?

  3. 例如:

    my $rrc_trans_identifier = $parser->{'c1'}->{'rrcConnectionSetupComplete'}->{'rrc-TransactionIdentifier'};
    print "rrc_trans_id :: $rrc_trans_identifier\n";
    

1 个答案:

答案 0 :(得分:3)

您需要编写自己的MySAXHandler(名称中的线索!)。

有关详细信息,请参阅SAX::Intro文档。