views:

445

answers:

1

I'm using ActiveSupport's to_xml to generate some xml from a hash.

I have need for the this ruby:

:Shipment => {
  :Packages => [
    {:weight => '5', :type => 'box'},
    {:weight => '3', :type => 'tube'}
  ]
}

To generate this xml:

<Shipment>
  <Package>
    <weight>5</weight>
    <type>box</type>
  </Package>
  <Package>
    <weight>3</weight>
    <type>tube</type>
  </Package>
</Shipment>

But instead, it wraps the array in another set of xml tags like this:

<Shipment>
  <Packages>
    <Package>
      <weight>5</weight>
      <type>box</type>
    </Package>
    <Package>
      <weight>3</weight>
      <type>tube</type>
    </Package>
  </Packages>
</Shipment>

Please don't tell me I need to change my xml structure... It's how UPS says to do it :(

Anybody know a work-around?

+4  A: 

Checking out builder is the way to go. Your xml.builder will look something like:

xml.shipment do
    @packages.each do |package|
        xml.package do
            xml.weight package.weight
            xml.type package.type
        end
    end
end
ghoppe