Hello, Monks! I have a problem to build a tree-like data structure from XML file fragment. I must use XML::SAX for it only. XML looks like this:

<menu id="3" name="menu_3" type="tree"> <template>menu_tree.tmpl</template> <parts> <part id="1" var="about"> <name>About project</name> <alt></alt> <part id="2" var="author"> <name>Authors</name> <alt></alt> </part> <part id="3" var="oferta"> <name>Public oferta</name> <alt></alt> </part> </part> <part id="4" var="for_user"> <name>For user</name> <alt></alt> <part id="5" var="gb"> <name>Guest book</name> <alt></alt> </part> <part id="6" var="reg"> <name>Registration</name> <alt></alt> </part> <part id="7" var="subscribe"> <name>Subscribe</name> <alt></alt> </part> </part> </parts> </menu>

In result I want to get the structure like below:

$struct = [ { id => 1, var => 'about', name => 'About project', alt => '', childs => [ { id => 2, var => 'author', name => 'Authors', alt => '' }, { id => 3, var => 'oferta', name => 'Public oferta' +, alt => '' } ] }, { id => 4, var => 'for_user', name => 'For user', alt => '', childs => [ { id => 5, var => 'gb', name => 'Guest book', alt => '', }, { id => 6, var => 'reg', name => 'Registration', alt => '' }, { id => 7, var => 'subscribe', name => 'Subscribe', alt => '' } ] } ];

I have a trouble with writing XML::SAX handlers for build this structure. Any suggestions? Thanks.

In reply to Building simple tree from XML by shady

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.