Hi Monks, I am looking to parse this demo file that is highly nested. the problem is that I need data from different nest levels. My XML file and sample code are below. I apologize for the XML file being so long since I wanted to show the complexity of the file. I am currently using XML twig to parse this. I will be more than happy to try other options. Basically I want my output to be of the form
d1|d2|Nest1->Nest2->d5->X|Nest1->Nest2->d5->Y|Nest1->Nest2->d6->X|Nest +1->Nest2->d6->Y|Nest1->Nest2->Nest3->Nest4->d7->d9->d10->text.
I am not able to retain the parent path information only for the required my data. My code prints a whole lot of junk data as well. Would appreciate any help. Thanks Monks!
Shorter XML data to make it easier
<DatatoParse> <elt> <d1>TV show 1</d1> <d2>Heroes</d2> <d3>4</d3> <d4/> <Nest1> <elt> <Junk1>FULL</Junk1> <Junk2>Page 65</Junk2> <Nest2> <elt> <d5> <X>-2</X> <Y>-3</Y> </d5> <d6> <X>5</X> <Y>8</Y> </d6> <Nest3> <Nest4> <d7> <d9> <d10> <d11>yipppeee</d11> </d10> </d9> </d7> </Nest4> <Junk> <d7> <d9> <d10> <d11>yipppeee</d11> </d10> </d9> </d7> </Junk> </Nest3> </elt> <elt> <d5> <X>-2</X> <Y>-3</Y> </d5> <d6> <X>5</X> <Y>8</Y> </d6> <Nest3> <Nest4> <d7> <d9> <d10> <d11>yipppeee</d11> </d10> </d9> </d7> </Nest4> <Junk> <d7> <d9> <d10> <d11>yipppeee</d11> </d10> </d9> </d7> </Junk> </Nest3> </elt> <elt> <d5> <X>-2</X> <Y>-3</Y> </d5> <d6> <X>5</X> <Y>8</Y> </d6> <Nest3> <Nest4> <d7> <d9> <d10> <d11>yipppeee</d11> </d10> </d9> </d7> </Nest4> <Junk> <d7> <d9> <d10> <d11>yipppeee</d11> </d10> </d9> </d7> </Junk> </Nest3> </elt> <elt> <d5> <X>-2</X> <Y>-3</Y> </d5> <d6> <X>5</X> <Y>8</Y> </d6> <Nest3> <Nest4> <d7> <d9> <d10> <d11>yipppeee</d11> </d10> </d9> </d7> </Nest4> <Junk> <d7> <d9> <d10> <d11>yipppeee</d11> </d10> </d9> </d7> </Junk> </Nest3> </elt> </Nest2> </elt> </Nest1> <notrequired1> <elt> <Junk1>FULL</Junk1> <Junk2>Page 65</Junk2> <Nest2> <elt> <d5> <X>-2</X> <Y>-3</Y> </d5> <d6> <X>5</X> <Y>8</Y> </d6> <Nest3> <Nest4> <d7> <d9> <d10> <d11>yipppeee</d11> </d10> </d9> </d7> </Nest4> <Junk> <d7> <d9> <d10> <d11>yipppeee</d11> </d10> </d9> </d7> </Junk> </Nest3> </elt> <elt> <d5> <X>-2</X> <Y>-3</Y> </d5> <d6> <X>5</X> <Y>8</Y> </d6> <Nest3> <Nest4> <d7> <d9> <d10> <d11>yipppeee</d11> </d10> </d9> </d7> </Nest4> <Junk> <d7> <d9> <d10> <d11>yipppeee</d11> </d10> </d9> </d7> </Junk> </Nest3> </elt> <elt> <d5> <X>-2</X> <Y>-3</Y> </d5> <d6> <X>5</X> <Y>8</Y> </d6> <Nest3> <Nest4> <d7> <d9> <d10> <d11>yipppeee</d11> </d10> </d9> </d7> </Nest4> <Junk> <d7> <d9> <d10> <d11>yipppeee</d11> </d10> </d9> </d7> </Junk> </Nest3> </elt> <elt> <d5> <X>-2</X> <Y>-3</Y> </d5> <d6> <X>5</X> <Y>8</Y> </d6> <Nest3> <Nest4> <d7> <d9> <d10> <d11>yipppeee</d11> </d10> </d9> </d7> </Nest4> <Junk> <d7> <d9> <d10> <d11>yipppeee</d11> </d10> </d9> </d7> </Junk> </Nest3> </elt> </Nest2> </elt> </notrequired1> </elt> <elt> <d1>TV show 2</d1> <d2>Prison Break</d2> <d3>8</d3> <d4/> <Nest1> <elt> <Junk1>FULL</Junk1> <Junk2>Page 65</Junk2> <Nest2> <elt> <d5> <X>-2</X> <Y>-3</Y> </d5> <d6> <X>5</X> <Y>8</Y> </d6> <Nest3> <Nest4> <d7> <d9> <d10> <d11>yipppeee</d11> </d10> </d9> </d7> </Nest4> <Junk> <d7> <d9> <d10> <d11>yipppeee</d11> </d10> </d9> </d7> </Junk> </Nest3> </elt> <elt> <d5> <X>-2</X> <Y>-3</Y> </d5> <d6> <X>5</X> <Y>8</Y> </d6> <Nest3> <Nest4> <d7> <d9> <d10> <d11>yipppeee</d11> </d10> </d9> </d7> </Nest4> <Junk> <d7> <d9> <d10> <d11>yipppeee</d11> </d10> </d9> </d7> </Junk> </Nest3> </elt> </Nest2> </elt> </Nest1> <notrequired1> <elt> <Junk1>FULL</Junk1> <Junk2>Page 65</Junk2> <Nest2> <elt> <d5> <X>-2</X> <Y>-3</Y> </d5> <d6> <X>5</X> <Y>8</Y> </d6> <Nest3> <Nest4> <d7> <d9> <d10> <d11>yipppeee</d11> </d10> </d9> </d7> </Nest4> <Junk> <d7> <d9> <d10> <d11>yipppeee</d11> </d10> </d9> </d7> </Junk> </Nest3> </elt> <elt> <d5> <X>-2</X> <Y>-3</Y> </d5> <d6> <X>5</X> <Y>8</Y> </d6> <Nest3> <Nest4> <d7> <d9> <d10> <d11>yipppeee</d11> </d10> </d9> </d7> </Nest4> <Junk> <d7> <d9> <d10> <d11>yipppeee</d11> </d10> </d9> </d7> </Junk> </Nest3> </elt> </Nest2> </elt> </notrequired1> </elt> </DatatoParse>
And here is the code I have so far
use warnings; use XML::Twig; use XML::Simple; $localfile= "Test_1.xml"; my $field = "Nest1"; open my $fout1, '>', "testx.csv" or die "Could not open file!"; $twig = XML::Twig->new( twig_roots => { $field => 1, 'd1' => 1, 'd2'=> 1, }, twig_handlers => { 'DatatoParse' => \&node, 'DatatoParse//*' => \&node1 } ); $twig->parsefile($localfile); 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"); }

In reply to 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.