Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^2: Easy XML-parser that can handle large file?

by choroba (Cardinal)
on Sep 11, 2014 at 07:52 UTC ( [id://1100264]=note: print w/replies, xml ) Need Help??


in reply to Re: Easy XML-parser that can handle large file? ( XML::Rules )
in thread Easy XML-parser that can handle large file?

To use XML::LibXML on large files, use the pull parser XML::LibXML::Reader. For example, the following script
#!/usr/bin/perl use warnings; use strict; use XML::LibXML::Reader; my $reader = 'XML::LibXML::Reader'->new( location => shift ) or die "Can't open file.\n"; while ($reader->read) { if ('product' eq $reader->name) { my $dom = $reader->copyCurrentNode(1); print $dom->findvalue('product_id'), "\n"; for my $attr ($dom->findnodes('attributes/attribute')) { my $groups = $attr->findnodes('group'); for my $group (@$groups) { my $gid = $group->findnodes('id'); my $gname = $group->findnodes('name'); print "\tgroup [ $gid : $gname ]\n"; } my $values = $attr->findnodes('value'); for my $value (@$values) { my $vid = $value->findnodes('id'); my $vval = $value->findnodes('value'); print "\tvalue [ $vid : $vval ]\n"; } } } }

produces the following output:

ABC123 group [ 1507 : Engines ] value [ 301 : Generator ] group [ 1561 : Längd (i mm) ] value [ : 2625 ] group [ 1498 : Year model ] value [ : 01.1994 ] group [ 1518 : Year model (to) ] value [ : 12.1998 ] group [ 12033 : Vehicle equipment ] value [ 12019 : Maybe ] XYZ789 group [ 1507 : Engines ] value [ 301 : Generator ] group [ 1498 : Year model ] value [ : 01.1985 ] group [ 1518 : Year model (to) ] value [ : 12.1992 ]
لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1100264]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (3)
As of 2024-03-29 01:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found