Boss Nass
Boss Nass

Reputation: 3522

Custom XML file from Rails App

I am trying to get my rails app to produce an XML file in a certain format, thus far I have app the churning out the correct fields using

format.xml { render xml:  @agencies }

which produces

<agencies type="array">
<agency>
<address>12 dansu ct</address>
<created-at type="datetime">2013-09-17T14:03:11Z</created-at>
<email>[email protected]</email>
<id type="integer">1</id>
<imageBgUrl>another.jpg</imageBgUrl>
<imageThumbUrl>image.jpg</imageThumbUrl>
<latitude type="float">12.4</latitude>
<longitude type="float">12.43</longitude>
<telNo>94959525</telNo>
<title>Paul</title>
<updated-at type="datetime">2013-09-17T14:03:11Z</updated-at>
<zipCode>3210</zipCode>
</agency>
</agencies>

though I need it to look like

<?xml version="1.0" encoding="UTF-8"?>
<entries type="array">
    <entry>
        <address>12 dansu ct</address>
        <created-at type="datetime">2013-09-17T14:03:11Z</created-at>
        <email>[email protected]</email>
        <id type="integer">1</id>
        <imageBgUrl>another.jpg</imageBgUrl>
        <imageThumbUrl>image.jpg</imageThumbUrl>
        <latitude>37.784062</latitude>
        <longitude>122.391579</longitude>
        <telNo>94959525</telNo>
        <title>Paul</title>
        <updated-at type="datetime">2013-09-17T14:03:11Z</updated-at>
        <zipCode>3210</zipCode>
    </entry>
</entries>

Upvotes: 0

Views: 141

Answers (2)

Zrth
Zrth

Reputation: 38

Are you using the XML Builder?

Here's an example of how it's used:

  require 'rubygems'
  require_gem 'builder'

  builder = Builder::XmlMarkup.new(:target=>STDOUT, :indent=>2)
  builder.person { |b| b.name("Jim"); b.phone("555-1234") }
  #
  # Prints:
  # <person>
  #   <name>Jim</name>
  #   <phone>555-1234</phone>
  # </person>

Upvotes: 0

claptimes
claptimes

Reputation: 1717

You could use a templating engine like Rabl or create an index.xml.erb file to render.

Something like:

<?xml version="1.0" encoding="UTF-8"?>
<entries type="array">
    <%= render collection: @agencies, partial: 'agency', format: :xml %>
</entries>

May need to validate my syntax...

Upvotes: 1

Related Questions