in reply to 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 C1 C2 C3 ); use XML::LibXML::Reader; my $reader = XML::LibXML::Reader->new(location => 'file.xml') or die; my %line1; while ($reader->read) { next unless $reader->nodeType == XML_READER_TYPE_ELEMENT; my $name = $reader->name; if (grep $_ eq $name, @els) { $line1{$name} = $reader->copyCurrentNode(1)->textContent; } elsif ('Author' eq $name) { print join "\t", map $_ // '??', @line1{@els}; %line1 = (); print "\n", $reader->copyCurrentNode(1)->textContent, "\n"; } elsif ('Publisher' eq $name) { print $reader->copyCurrentNode(1)->textContent, "\n"; } }
Update: Or, similarly, using stream from XML::XSH2:
stream :f 'file.xml' :F '/dev/null' select elt { echo :s (Book) {"\t"} (Snap) {"\t"} (Line1) {"\t"} (Line2) {"\t"} (Line3) {"\t"} (Line4) {"\t"} (Line5) {"\t"} (Rating/C1) {"\t"} (Rating/C2) {"\t"} (Rating/C3) ; } select Author { echo (.) ; } select Publisher { echo (.) ; } ;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: how to display descendants in XML with Perl's XML::Twig
by Ppeoc (Beadle) on Oct 16, 2015 at 13:20 UTC | |
by choroba (Cardinal) on Oct 16, 2015 at 13:45 UTC | |
by Ppeoc (Beadle) on Oct 16, 2015 at 16:12 UTC | |
by Ppeoc (Beadle) on Oct 16, 2015 at 17:35 UTC | |
by poj (Abbot) on Oct 16, 2015 at 20:33 UTC | |
by choroba (Cardinal) on Oct 18, 2015 at 20:31 UTC |