Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

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

by choroba (Bishop)
on Sep 11, 2014 at 07:52 UTC ( #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
Node Status?
node history
Node Type: note [id://1100264]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2019-03-26 22:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How do you Carpe diem?





    Results (125 votes). Check out past polls.

    Notices?