in reply to how to display descendants in XML with Perl's XML::Twig

Using alternative module, XML::LibXML::Reader:
#!/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
    Thanks for the code. The problem with using this is that the XML file I am working on is 300 mb and highly nested. Each child has diffrent levels of nesting. The if else would be tedious to use. My other question What if I had nested children within nested children? What would be the most efficient to do it? For example how do I access the elements of elt for each C? My 2nd question is about how I could display these elements like
    The book of pages|Snap|Line1|Line2|Line3|Line4|Line5|C1.X| The book of pages|Snap|Line1|Line2|Line3|Line4|Line5|C1.Y| The book of pages|Snap|Line1|Line2|Line3|Line4|Line5|C2.X| The book of pages|Snap|Line1|Line2|Line3|Line4|Line5|C2.Y| The book of pages|Snap|Line1|Line2|Line3|Line4|Line5|C3.X| The book of pages|Snap|Line1|Line2|Line3|Line4|Line5|C3.Y| . . . . . The song|Snap|Line1|Line2|Line3|Line4|Line5|C2.X| The song|Snap|Line1|Line2|Line3|Line4|Line5|C2.Y| Example <Rating> <C1> <elt> <X></X> <X></X> </elt> <elt> <elt> </C1> <C2> <elt> <elt> <elt> </C2> <C3> <elt> <elt> <elt> </C3> </Rating>
    Is there another XML module that can be more helpful than XML::Twig for this purpose?
      I don't understand. Can you post a bit bigger input I can test my code against? Use the <readmore> tags to save readers from excessive scrolling.
      لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
        Updated the question. I hope this makes more sense. Sorry I cant display a bigger part of the code because of IP
        Edited the question. Hope it makes more sense