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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com To unsubscribe from this group, send email to rubyonrails-core-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en -~----------~----~----~----~------~----~------~--~---
Would this patch include modifying the request param parser to reconstitute a model out of this alternate xml format? Ben On Mar 23, 2008, at 9:24 AM, Nate Wiger wrote:> > 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 > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com To unsubscribe from this group, send email to rubyonrails-core-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en -~----------~----~----~----~------~----~------~--~---
On Mar 23, 2008, at 9:24 AM, Nate Wiger wrote:> > 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, > NateThis 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. -- Josh Susser http://blog.hasmanythrough.com --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com To unsubscribe from this group, send email to rubyonrails-core-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en -~----------~----~----~----~------~----~------~--~---
carlos@milk-it.net
2008-Mar-24 00:48 UTC
Re: New feature offering: to_xml(:compact => true)
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... On 3/23/08, Josh Susser <josh@hasmanythrough.com> wrote:> > > On Mar 23, 2008, at 9:24 AM, Nate Wiger wrote: > > > > 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 > > > 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. > > -- > Josh Susser > http://blog.hasmanythrough.com > > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com To unsubscribe from this group, send email to rubyonrails-core-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en -~----------~----~----~----~------~----~------~--~---