in reply to Re^4: how to display descendants in XML with Perl's XML::Twig
in thread how to display descendants in XML with Perl's XML::Twig
#!/usr/bin/perl use warnings; use strict; my @els = qw( Book Snap Line1 Line2 Line3 Line4 Line5 ); use XML::LibXML::Reader; my $reader = XML::LibXML::Reader->new(location => 'file.xml') or die; my (%line, $C); while ($reader->read) { next unless $reader->nodeType == XML_READER_TYPE_ELEMENT; my $name = $reader->name; if (grep $_ eq $name, @els) { $line{$name} = $reader->copyCurrentNode(1)->textContent; } elsif ($name =~ /^C[123]$/) { $C = $name; } elsif ($name =~ /^[XY]$/) { push @{ $line{$C}{$name} }, $reader->copyCurrentNode(1)->textC +ontent; } elsif ('Author' eq $name) { for my $c (qw( C1 C2 C3 )) { for my $letter (qw( X Y )) { print join '|', @line{@els}, "$c.$letter", @{ $line{$c +}{$letter} }; print "\n"; } } %line = (); } }
Output:
The book of pages||The Beginning|We ceased to exist|Accept it|Now we l +ive|We reject it|C1.X|10.5|3.5|10.5|10.5|10.5|10.5 The book of pages||The Beginning|We ceased to exist|Accept it|Now we l +ive|We reject it|C1.Y|11.4|13.4|11.4|11.4|11.4|11.4 ...
|
|---|