Project

ox-mapper

0.0
No commit activity in last 3 years
No release in over 3 years
Create SAX parsers based on `ox` with simple DSL
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

Runtime

>= 0
 Project Readme

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) # => {: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.

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request