shady has asked for the wisdom of the Perl Monks concerning the following question:

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.

Replies are listed 'Best First'.
Re: Building simple tree from XML
by pajout (Curate) on Oct 13, 2005 at 08:56 UTC
    I think that the reason of your issues is that required structure is NOT equivalent with input xml (and not easily relatable with SAX events). For instance, your 'name' key is the name of child element, but 'id' is attribute...
    If you don't want use previously adviced tools, write handlers of SAX events, which builds some generic structure, without any ideas about xml dtd, and after that transform that structure into required one.
Re: Building simple tree from XML
by Skeeve (Parson) on Oct 12, 2005 at 17:31 UTC

    Did you read XML::SAX?

    Did you try to implement handlers?

    If so, where are they? What are the problems you encountered?

    Or do you expect the Monks to do your work for free? ;-)


    s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%.+=%;.#_}\&"^"-+%*) +.}%:##%}={~=~:.")&e&&s""`$''`"e
Re: Building simple tree from XML
by mirod (Canon) on Oct 12, 2005 at 19:41 UTC
    I must use XML::SAX for it only

    Why?

      This way I can to decrease memory usage while processing a big XML files.
        ...decrease memory usage while processing a big XML files.
        That is what XML::Twig does handily.

        But you seem to be building a tree in memory that would then be quite big too. So either I am missing something, or you gave us too little information to really help you.

        Try XML::Twig. It's very easy to use and you can throw away parsed data as soon as you are done with it.

        s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
        +.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e