0

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>

2 Answers 2

1

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...

Sign up to request clarification or add additional context in comments.

Comments

0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.