Ruby on Rails XML Builder



While working on a recent update to BlueGPS I’ve added the ability to “geotag” favorite spots with a location and basic note. The data is uploaded from a Windows Mobile device to a remote server. The server is currently running Ruby on Rails. I’ve added methods to add geotags and am currently working on the interface to display them. Since these tags will ultimately live on a Google map it makes sense to deliver them as XML and load them via AJAX.

Much to my suprise Rails provides just the mechanism to get data in an XML format. This is known as the Rails Builder. Generating XML is as simple as specifying a .rxml template instead of .rhtml. For example, my controller has an action of get_locations and instead of specifying a view name of get_locations.rhtml, I used get_locations.rxml. The builder code looks like:

xml.locations do
@locations.each do |temp|
xml.location(:lat => temp.latitude, :lon => temp.longitude, :note => temp.note) do
end
end
end

and generates the following XML:

[locations]
[location note="Ella's favorite little donut shop." lat="3250.1878,N" lon="09645.8498,W"][/location]
[location note="Golf Galaxy superstore. A good place to blow all your money!" lat="3252.5340,N" lon="09646.1594,W"][/location]
[location note="Matt's Rancho Martinez. Great Mexican food!" lat="3248.7498,N" lon="09645.2135,W"][/location]
[location note="SensorLogic Headquarters" lat="3257.5224,N" lon="09649.4056,W"][/location]
[/locations]

These are actual tags I generated over the weekend using the next version of BlueGPS. I’ll be working to display this data on a Google map in the next few days. The next version of BlueGPS will support geotagging and I’m hoping users will share their favorite spots with the community.



One Response to “Ruby on Rails XML Builder”

  1. Thanks, this just gave me a great idea on how to add a map based search to the addresses within our hosted CRM XLsuite.

Leave a Reply