Skip to content

happymappervsox

MIT 24 7 151
28.1 thousand (month) Mar 19 2010 0.10.0(11 months ago)
901 4 13 MIT
Jul 01 2011 161.4 thousand (month) 2.14.18(8 months ago)

Happymapper allows you to parse XML data and convert it quickly and easily into ruby data structures.

Happymapper maps XML structures to ruby data structures which then can be parsed for data using any other Ruby data tools. This also means, happymaker can convert XML to JSON as ruby data structures are easily exportable to JSON

Ox is a Ruby gem that provides a simple and efficient way to parse and generate XML and JSON documents. It is built on top of the underlying C library libox, which is known for its speed and low memory usage. Ox provides a simple and intuitive API for parsing and generating XML and JSON documents, and it is widely used in the Ruby ecosystem for web scraping, web development, and data-gathering applications.

One of the main features of Ox is its ability to parse and generate XML and JSON documents with low memory usage. Ox uses a pull-based parsing strategy, which allows it to parse large documents without loading the entire document into memory, thus making it more efficient than other parsing libraries. Ox also provides a fast and efficient way of generating XML and JSON documents.

Ox also provides a variety of other features that can simplify the process of working with XML and JSON documents. It can automatically handle character encodings, it can parse and generate documents with minimal memory usage, and it can validate documents against a DTD or schema.

Example Use


require 'happymapper'

xml = '<address>
  <street>Milchstrasse</street>
  <housenumber>23</housenumber>
  <postcode>26131</postcode>
  <city>Oldenburg</city>
  <country code="de">Germany</country>
</address>'

# we can parse it as is:
address = HappyMapper.parse(ADDRESS_XML_DATA)
address.street # => Milchstrasse
address.housenumber # => 23
address.postcode # => 26131
address.city # => Oldenburg
address.country.code # => de
address.country.content # => Germany

# or define a structure
class Address
  include HappyMapper

  tag 'address'
  element :street, String, tag: 'street'
  element :postcode, String, tag: 'postcode'
  element :housenumber, Integer, tag: 'housenumber'
  element :city, String, tag: 'city'
  element :country, String, tag: 'country'
end
Address.parse(xml)
require 'ox'

class Sample
  attr_accessor :a, :b, :c

  def initialize(a, b, c)
    @a = a
    @b = b
    @c = c
  end
end

# Create Object
obj = Sample.new(1, "bee", ['x', :y, 7.0])
# Now dump the Object to an XML String.
xml = Ox.dump(obj)
# Convert the object back into a Sample Object.
obj2 = Ox.parse_obj(xml)

Alternatives / Similar


Was this page helpful?