Working with XML in Rails

Hi there I am working on a project where I need to implement some XML, is there someone here who knows what the best way is to do in Rails.

Sure, let’s talk? – H

Depends on your goal. What exactly do your mean by “implement some XML”? Do you mean return XML as a response?

I would think a .builder file is the easiest way. This is build on top of the builder (which I think is now maintained as a fork by @tenderlove) and built into Rails.

The to_xml object can also be useful on many objects as a simple way to render xml. So:

render xml: my_object

There are various way to exclude fields, add methods, etc for ActiveRecord objects and collections. I believe to_xml is built on top of the builder gem.

Also Ruby itself has REXML built-in so that’s nice with no extra dependencies.

Outside that there are many 3rd party libraries. The bindings to libxml is probably a top pick. Also nokogirl which wraps libxml but with a much more friendly API is commonly used. Finally if you need something for a specific purpose there might be a library already for that. For example savon provides a SOAP library.

1 Like