Ox::Mapper
Ox::Mapper's intention is to simplify creation of parsers based on ox
Installation
Add this line to your application's Gemfile:
gem 'ox-mapper'
And then execute:
$ bundle
Or install it yourself as:
$ gem install ox-mapper
Usage
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>\n <book>\n <title>Serenity</title>\n <author name=\"John Dow\" age=\"99\" />\n <price value=\"1123\" />\n </book>\n </xml>\n") # => {:title => "Serenity", :author => "John Dow", :price => 1123.0}
This API is unstable and a subject to change.
Contributing
- 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