Saturday, April 19, 2008

Massage Rails better_nested_set full_set into a hierarchy - XML

In a previous post I showed how to convert a betternestedset full_set into a JSON hierarchy that you might feed to a javascript or flash component. Another thing you might want to do is to massage the data into an XML hierarchy to feed a ... javascript or flash component. Now you could use similar code to the previous example and build the XML document up with REXML or another XML library and be done with it. You may, however, want to avoid using such an XML library to avoid the speed penalty that comes with it. Here is an example of converting the content returned from full_set into an XML document without using an XML library:


  1. hi, im a newbie in rails but in would like to utilize ur method. could u help me? my question is how to get that xml file back from the browser?
    hiow is that connected to the view? could u give a whole example? thx so much!
    best tom

  2. Sure Tom how about an action like this:

    def some_action
    @message = Message.find(params[:id])
    respond_to do |format|
    format.html {}
    format.xml {
    render :xml => @message.full_xml

    That is assuming that a request expecting XML wants this full XML result.