New feature offering: to_xml(:compact => true)

We're using Rails to feed XML to a small embedded-Linux device. This device has very strict memory requirements, so we've have to compact the XML output. The default Rails format uses elements for attributes:

  <users type="array">      <user>         <id>6</id>         <name>Steve</name>      </user>      <user>          <id>7</id>         <name>Jim</name>      </user>   </users>

I added a ":compact" option to to_xml that changes this format to use XML attributes for AR attributes:

  format.xml => { render :xml => @users.to_xml(:compact => true) }

This creates:

  <users type="array">      <user id="6" name="Steve"/>      <user id="7" name="Jim"/>   </users>

Another nice side effect of this is that the client DOM calls are simpler; eg, just getAttribute() as opposed to findChildElement, getContent, etc.

Is there interest in Rails core for this? If so, I can cleanup the code and submit it as a patch.

Thanks, Nate

Would this patch include modifying the request param parser to
reconstitute a model out of this alternate xml format?

Ben

This approach may work for objects that have only primitive data types as their attributes, but it fails when serializing objects that have attributes that themselves are objects with their own attributes. For that reason, I don't think this is useful as a general way to compact the XML output. There's also the issue of how to parse the incoming XML, and how to distinguish between the two XML formats. Removing unnecessary white space is probably the simplest thing you can do to reduce the size of the generated XML. Anything else is an adventure in plugin-land.

Maybe is a good idea release it as a plugin, and for the failing case you could do something like:

<users type="array">      <user id="6" name="Steve">          <complex_object attribute="value" />      </user>      <user id="7" name="Jim"/>   </users>

but this would be a pattern problem...