Why am I getting "Out of memory" error with Perl XML :: Simple?

Edited::

Hello everybody,

I have such an XML file,

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

I use Perl code like this to access data in an XML file (I have to adhere to this access format)

#!/usr/bin/perl

use strict;

use XML::Simple;

my $xml = new XML::Simple;

my $data = $xml->XMLin("uL-DCCH-Message.xml");

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

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

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

The output:

rrc_trans_id :: 2
plmn identity ::  1
dedicated info nas ::  07410109014290112345671000028020000f0

Perl code using XML :: Simple works fine for small XML files (as shown in the above output).

But if the XML file is large, then XML :: Simple cannot process and displays the Ran out of memory error message.

My question is: are there any other XML parsers that I can use so that I can access the elements in the XML file in the same way as shown above ???

If there are other parsers available, can anyone give an example by following the same conventions as for XML :: Simple ..

, ,

+3
4

, XML::Twig

use strict;
use warnings;
use XML::Twig;

XML::Twig->new(
    twig_handlers => {
        'c1/rrcConnectionSetupComplete' => \&my_stuff,
    }
)->parsefile( 'uL-DCCH-Message.xml' );

sub my_stuff {
    my ($twig, $elt) = @_;

    my $rrc_trans_identifier = $elt->field( 'rrc-TransactionIdentifier' );
    print "rrc_trans_id :: $rrc_trans_identifier\n";

    my $twiglet = $elt->first_child('criticalExtensions')
                      ->first_child('c1')
                      ->first_child('rrcConnectionSetupComplete-r8');


    my $selected_plmn_id = $twiglet->first_child_text('selectedPLMN-Identity');
    print "plmn identity :: $selected_plmn_id\n";    

    my $rrc_dedicated_info_nas = $twiglet->first_child_text('dedicatedInfoNAS');
    print "dedicated info nas :: $rrc_dedicated_info_nas\n";

    $twig->purge;
}

, :

rrc_trans_id :: 2
plmn identity ::  1 
dedicated info nas ::  07410109014290112345671000028020000f0 


!

/I3az/

+2

, . , XML , , . XML- , XML::Twig.

, XML , , . , - :

#!/usr/bin/perl

use strict;
use warnings;

use XML::Twig;

sub process_record {
    my $rec = shift;
    print "@{$rec}{qw/field1 field2 field3/}\n";
}

my $t = XML::Twig->new(
    twig_handlers => {
        record => sub {
            my ($t, $elt) = @_;

            my %record = map {
                $_ => $elt->first_child($_)->text
            } qw/field1 field2 field3/;

            process_record(\%record);

            #get rid of any memory that is being held
            $t->purge;
        },
    }
);

$t->parse(\*DATA);

__DATA__
<root>
    <record>
        <field1>1</field1>
        <field2>a</field2>
        <field3>foo</field3>
    </record>
    <record>
        <field1>2</field1>
        <field2>b</field2>
        <field3>bar</field3>
    </record>
    <record>
        <field1>3</field1>
        <field2>c</field2>
        <field3>baz</field3>
    </record>
</root>
+11

, XML:: Simple , , - . , XML. , , .

, . , . , , .:)

+3

XML SAX-, XML::SAX. . Perldoc XML:: SAX:: Intro.
XML::Simple , . .

+2

Source: https://habr.com/ru/post/1765085/


All Articles