happymappervsroxml
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
ROXML is a Ruby gem that provides a simple and intuitive way to map XML documents to Ruby objects, and vice versa. It provides a simple and intuitive API for defining the mapping between the XML and the Ruby object, and it uses Nokogiri to parse and search the XML documents.
One of the main features of ROXML is its ability to map elements and attributes of an XML document to properties of a Ruby object, and vice versa. It allows you to define the mapping between the elements or attributes and the properties using a simple and intuitive API, and then automatically applies the mapping to the document, creating an instance of the Ruby object with the properties set to the values from the XML document. It also provides a way to map a Ruby object to an XML document.
ROXML also provides a variety of other features that can simplify the process of working with XML documents. It can handle nested elements, it can handle arrays of elements, and it can handle different data types. It also provides a built-in support for parsing XML using Nokogiri.
ROXML is also known to be more flexible and powerful than other libraries like HappyMapper because it also allows to map a Ruby object to an XML document and it's more flexible when it comes to handling of arrays, and nested elements.
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 'roxml'
class Book
include ROXML
xml_name :book
xml_accessor :title, :from => '@title'
xml_accessor :author, :from => 'author'
end
xml_string = '<book title="The Great Gatsby">
<author>F. Scott Fitzgerald</author>
</book>'
book = Book.from_xml(xml_string)
puts book.title # "The Great Gatsby"
puts book.author # "F. Scott Fitzgerald"