in reply to Parsing a highly nested XML file correctly and efficiently
I am currently using XML twig to parse this. I will be more than happy to try other options.
There are other (few) options, but not XML::Simple. Avoid it. I suspect that you unhappiness is due not to XML::Twig but to the shaggy beast XML is, per se.
Anyway i do not understand your expected output format:
d1|d2|Nest1->Nest2->d5->X|Nest1->Nest2->d5->Y|Nest1->Nest2->d6->X|Nest1->Nest2->d6->Y|Nest1->Nest2->Nest3->Nest4->d7->d9->d10->text.
what the above means?
Modifying a little your program (using strict too..)
use strict; #use this! use warnings; use XML::Twig; # use XML::Simple; NOOOO! # $localfile= "Test_1.xml"; used DATA instead my $field = "Nest1"; open my $fout1, '>', "testx.csv" or die "Could not open file!"; my $twig = XML::Twig->new( twig_roots => { $field => 1, 'd1' => 1, 'd2'=> 1, }, twig_handlers => { 'DatatoParse' => \&node, 'DatatoParse//*' => \&node1 } ); $/=''; ## added this $twig->parse(<DATA>); #modified this sub node { my($twig, $el) = @_; $twig->purge; } sub node1{ print $fout1 "\n", if ($_->tag eq "d1"); print $fout1 $_->text, ",", unless ($_->has_children('#EL +T')); print $fout1 "\n", if ($_->tag eq "elt"); } __DATA__ <DatatoParse> <elt> ....
I obtain some output that make some sense and no garbage at all:
#cat testx.csv TV show 1,Heroes,FULL,Page 65,-2,-3,5,8,yipppeee,yipppeee, -2,-3,5,8,yipppeee,yipppeee, -2,-3,5,8,yipppeee,yipppeee, -2,-3,5,8,yipppeee,yipppeee, TV show 2,Prison Break,FULL,Page 65,-2,-3,5,8,yipppeee,yipppeee, -2,-3,5,8,yipppeee,yipppeee, -2,-3,5,8,yipppeee,yipppeee, -2,-3,5,8,yipppeee,yipppeee, TV show 4,Alias,FULL,Page 65,-2,-3,5,8,yipppeee,yipppeee, -2,-3,5,8,yipppeee,yipppeee, -2,-3,5,8,yipppeee,yipppeee, -2,-3,5,8,yipppeee,yipppeee,
What is wrong with this? What output you want?
L*
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Parsing a highly nested XML file correctly and efficiently
by Ppeoc (Beadle) on Jun 09, 2016 at 16:14 UTC | |
by Discipulus (Canon) on Jun 10, 2016 at 08:23 UTC |