rails xml to active record object
I've been googling for a while to try and convert and incoming XML request into an active record object. I've tried using the ActiveRecordObject.new.from_xml method but it doesn't seem to handle relationships.
For example, say I have the following xml:
<blog>
<title></title>
<blog-pages>
<blog-page>
<page-number></page-number>
<content></content>
</blog-page>
</blog-pages>
</blog>
And I have the followi开发者_StackOverflow中文版ng model objects:
class Blog < ActiveRecord::Base
has_many :blog_pages
end
class BlogPage < ActiveRecord::Base
belongs_to :blog
end
Is there a way to convert the xml into a blog object WITH relationships? Or do I need to manually parse the XML?
Thanks in advance.
I banged my head up against this for a while. It seems there's not much out there to map XML to ActiveRecord objects without doing it manually. While my needs were similar to yours I needed also needed to not just do one object with relationships, I needed to do many. I looked in a lot of places and couldn't find anything. I decided to solve it myself but realised that it wasn't going to be a one liner so wrote a gem that extended ActiveRecord. I based on the work by Wayne Robinson:
http://www.wayne-robinson.com/journal/2006/5/1/ruby-on-rails-activerecordbuild_from_xml-function.html
After a couple of months in my non existant spare time I finished my gem and you can check out at RubyGems: https://rubygems.org/gems/xml_active
Hope this helps :) Cheers,
Michael
UPDATE
xml_active has now been officially retired and development is now focused on data_active (see https://github.com/michael-harrison/data_active) which has the functionality of xml_active but in future releases I will be working to support other formats
Look here: http://www.wayne-robinson.com/journal/2006/5/1/ruby-on-rails-activerecordbuild_from_xml-function.html
精彩评论