Here is a way to do it using XML::LibXML. Using Xpath expressions you should be able to match your nesting exactly.
my $doc = XML::LibXML->load_xml(string => $xml); my @nodes = $doc->findnodes('DatatoParse/elt'); for my $node ( @nodes ) { my $d1 = $node->findvalue('d1'); my $d2 = $node->findvalue('d2'); my @xnodes = $node->findnodes( 'Nest1/elt/Nest2/elt' ); for my $xnode ( @xnodes ) { my $d5x = $xnode->findvalue( 'd5/X' ); my $d5y = $xnode->findvalue( 'd5/Y' ); my $d6x = $xnode->findvalue( 'd6/X' ); my $d6y = $xnode->findvalue( 'd6/Y' ); my $d10 = $xnode->findvalue( 'Nest3/Nest4/d7/d9/d10' ); $d10 =~ s/^\s+//; $d10 =~ s/\s+$//; print "$d1,$d2,$d5x,$d5y,$d6x,$d6y,$d10\n"; } }
Output:
TV show 1,Heroes,-2,-3,5,8,yipppeee TV show 1,Heroes,-2,-3,5,8,yipppeee TV show 1,Heroes,-2,-3,5,8,yipppeee TV show 1,Heroes,-2,-3,5,8,yipppeee TV show 2,Prison Break,-2,-3,5,8,yipppeee TV show 2,Prison Break,-2,-3,5,8,yipppeee TV show 2,Prison Break,-2,-3,5,8,yipppeee TV show 2,Prison Break,-2,-3,5,8,yipppeee TV show 4,Alias,-2,-3,5,8,yipppeee TV show 4,Alias,-2,-3,5,8,yipppeee TV show 4,Alias,-2,-3,5,8,yipppeee TV show 4,Alias,-2,-3,5,8,yipppeee

In reply to Re: Parsing a highly nested XML file correctly and efficiently by tangent
in thread Parsing a highly nested XML file correctly and efficiently by Ppeoc

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.