views:

649

answers:

2

Hello,

I work at a small company and our production system uses a hand-rolled RESTful API, implemented in Java with JAXB. We now find that we're taking on customers who use Ruby on Rails, and I have to come up with a reference implementation to show customers how to use our API in Ruby. I'd love to be able to just tell them to use ActiveResource, but the XML required by our API uses (and absolutely requires) namespaces. Unfortunately, we've already got a number of other customers who've already integrated this API, so removing the usage of namespaces is out of the question. What's the best way to generate XML with namespaces in Ruby ?

+9  A: 

"Best" obviously depends on your needs.

The fastest way to generate any XML in ruby is to use libxml-ruby - link to rdoc. If your server gets any kind of load at all, this will be the way to go.

The easiest way to generate any XML in ruby is to use REXML as it's part of the standard library and therefore it "just works". If your XML generation is something that hardly ever gets used, it's probably easier to just go with rexml.

Both support XML namespaces - check the rdocs to find out how to set and get namespaces

Orion Edwards
Performance isn't really an issue here, so I wound up going with REXML as it looked like the quickest, simplest solution and it worked pretty well. Thank you very much for your response.
Alex Marshall
+1  A: 

Hi Lucas,

I find myself in almost an identical situation as yours (RESTful API done with JAXB w/ namespaces).

I think the most promising project for working with XML in Ruby is HappyMapper. It is a kind of XML binding library (along the lines of an early JAXB-type implementation). It has been gaining a lot of traction recently, and a few of us have been working on providing good namespace support.

The project resides here: http://happymapper.rubyforge.org/

with the source here: http://github.com/jnunemaker/happymapper/tree/master

The project currently doesn't support creation of XML from Ruby Objects, and the original author has expressed no desire to provide that support, but I'll be committing some functionality for that in my fork: http://github.com/jimmyz/happymapper/tree/master

Hope this helps.

-- Jimmy Zimmerman