Ox::Mapper's intention is to simplify creation of parsers based on ox
Add this line to your application's Gemfile:
gem 'ox-mapper'
And then execute:
$ bundle
Or install it yourself as:
$ gem install ox-mapper
All you need to do is to setup callbacks for elements and attributes in Ruby style
mapper = Ox::Mapper.new
mapper.on(:book) { |book| puts book.attributes.inspect }
mapper.on(:title) { |title| title.parent[:title] = title.text }
# collected attributes should be set up explicitely
mapper.on(:author, :attributes => :name) { |e| e.parent[:author] = e[:name] }
# setup transformation for attribute "value" of "price" element
mapper.on(:price, :attributes => :value) { |e| e.parent[:price] = Float(e[:value]) }
mapper.parse(StringIO.new <<-XML) # => {:title => "Serenity", :author => "John Dow", :price => 1123.0}
<xml>
<book>
<title>Serenity</title>
<author name="John Dow" age="99" />
<price value="1123" />
</book>
</xml>
XML
This API is unstable and a subject to change.
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request