When using XML::Simple, it's usually very helpful to use something like Data::Dumper to help understand the structure of the data. From my personal experience, attributes are usually hash keys and multiple tags at a level are usually arrays.

Here's the code that I came up with to parse your XML data and run it through Data::Dumper.

use strict; use warnings; use XML::Simple; use Data::Dumper; my $file = "data.xml"; my $xml = XMLin($file); print Dumper($xml);

And here's the output.

$VAR1 = { 'levels' => { 'level' => { 'c' => { 'target' => '22.1', 'level' => { 'g' => { 'weight' => +'30.8', 'study' => { ' +p' => { + 'weight' => '75', + 'id' => '50' + }, ' +q' => { + 'weight' => '25', + 'id' => '70' + } } }, 'd' => { 'level' => { ' +e' => { + 'weight' => '44.4', + 'study' => { + 'l' => { + 'weight' => '50', + 'id' => '34' + }, + 'm' => { + 'weight' => '50', + 'id' => '27' + } + } + }, ' +f' => { + 'weight' => '55.6', + 'study' => { + 'n' => { + 'weight' => '60', + 'id' => '25' + }, + 'o' => { + 'weight' => '40', + 'id' => '38' + } + } + } }, 'weight' => +'69.2' } }, 'weight' => '13' }, 'h' => { 'target' => '19.0', 'weight' => '22', 'study' => { 'r' => { 'weight' => +'90.9', 'id' => '40' }, 's' => { 'weight' => +'9.1', 'id' => '22' } } }, 'b' => { 'target' => '35.1', 'weight' => '50', 'study' => { 'j' => { 'weight' => +'70', 'id' => '36' }, 'i' => { 'weight' => +'30', 'id' => '32' } } } }, 'target' => '29.6', 'name' => 'a', 'study' => { 'weight' => '15', 'name' => 'k', 'id' => '37' } }, 'study' => { 'u' => { 'spec' => 'p0f7=1', 'weight' => '0', 'id' => '19' }, 'v' => { 'weight' => '0', 'id' => '62' }, 't' => { 'spec' => 'p0f7=2', 'weight' => '0', 'id' => '19' } } };

That output should help you figure out how to write your code to traverse the data structure. If that's not a data structure that you're liking, you can check out the available options for the XMLin function or you might need to check out other XML parsing modules. Since I personally have only used XML::Simple, I can't really recommend any other modules to try.


In reply to Re: parsing multi level XML with XML::Simple by dasgar
in thread parsing multi level XML with XML::Simple by lcheung

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.