How to create XML :: Simple data structure using Perl XML SAX parser?

Summary . I am looking for a quick XML parser (most likely a wrapper around some standard SAX parser) that will create a 100% data structure per record that is identical to the ones XML :: Simple creates .

More details

We have a large code infrastructure that depends on the processing of records one by one and expects the record to be a data structure in the format created by XML :: Simple, since it always used XML :: Simple from the early Jurassic era.

Simple XML Example:

<root>
    <rec><f1>v1</f1><f2>v2</f2></rec>
    <rec><f1>v1b</f1><f2>v2b</f2></rec>
    <rec><f1>v1c</f1><f2>v2c</f2></rec>
</root>

And an example of rough code:

sub process_record { my ($obj, $record_hash) = @_; # do_stuff }
my $records = XML::Simple->XMLin(@args)->{root};
foreach my $record (@$records) { $obj->process_record($record) };

, XML: Simple, , . , hog &mdash, - , DOM / 100% . , XML , .

( process_record) SAX , , XML::.

, , , SAX ( - ), $record hashrefs XML, , $obj->process_record($record) 100% , XML:: Simple hashrefs.

, ; , next_record() , .

+3
3

XML::Twig , XML, , , :

, XML:: Simple

:

use XML::Twig;
use Data::Dumper;

my $twig = XML::Twig->new(
    twig_handlers => {
        rec => \&rec,
    }
)->parsefile( 'data.xml' );


sub rec {
    my ($twig, $rec) = @_;
    my $data = $rec->simplify;
    say Dumper $data;
    $rec->purge;
}

NB. $rec- > .

XML- :

$VAR1 = {
          'f1' => 'v1',
          'f2' => 'v2'
        };

$VAR1 = {
          'f1' => 'v1b',
          'f2' => 'v2b'
        };

$VAR1 = {
          'f1' => 'v1c',
          'f2' => 'v2c'
        };

, , XML:: Simple:)

/I3az/

+7

XML:: Simple, .

XML:: Simple - DOM, . SAX, XML:: Parser. , . "make test" XML:: Simple, .

- XML ​​:: SAX:: PurePerl, , , . XML:: Expat, XML:: ExpatXS . ( SAX, ).

, , -, , , .

, , , XML:: LibXML. DOM, , grunt C. XPath , XML:: Simple - . .

+6

Take a look at XML :: LibXML :: Reader .

0
source

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


All Articles