Introduction

XmlNuts is an library that allows for bidirectional mapping between Ruby objects and XML.

Released under the MIT license.

Features

  • Type awareness (extensible).

  • XML namespaces support.

  • Pluggable backends to work with different XML APIs (REXML implemented so far).

Installation

gem install pipa-xmlnuts --source http://gems.github.com

Usage

Please see an example below. See also XmlNuts::Nut, XmlNuts::Nut::ClassMethods

TODO

  • Inheritance

  • Mixins

  • More mappings?

  • More types?

Reporting bugs

Please file bugs and feature requests at pipa.lighthouseapp.com/projects/21756-xmlnuts

Example

class Cheezburger
  include XmlNuts::Nut

  attribute :weight, :integer
end

class Cat
  include XmlNuts::Nut

  namespaces :lol => 'urn:x-lol', :kthnx => 'urn:x-lol:kthnx'

  root 'kitteh', :xmlns => :lol

  attribute :has_tail, :boolean, :xmlname => 'has-tail', :xmlns => 'urn:x-lol:kthnx'
  attribute :ears, :integer

  element :ration, [:string], :xmlname => :eats, :xmlns => :kthnx
  element :name, :string, :xmlns => 'urn:x-lol:kthnx'
  elements :paws, :string, :xmlname => :paw

  element :friends, :xmlname => :pals do # anonymous class definition follows within block
    elements :names, :string, :xmlname => :friend, :xmlname => :pal
  end

  element :cheezburger, Cheezburger
end

xml_fragment = "    <kitteh xmlns='urn:x-lol' xmlns:kthnx='urn:x-lol:kthnx' ears=' 2 ' kthnx:has-tail=' yes  '>\n      <name xmlns='urn:x-lol:kthnx'>\n          Silly\n          Tom\n      </name>\n      <kthnx:eats>\n        tigers\n        lions\n      </kthnx:eats>\n      <pals>\n        <pal>Chrissy</pal>\n        <pal>Missy</pal>\n        <pal>Sissy</pal>\n      </pals>\n      <paw>  one</paw>\n      <paw> two </paw>\n      <paw>three</paw>\n      <paw>four</paw>\n      <cheezburger weight='2' />\n    </kitteh>\n"
cat = Cat.parse(xml_fragment)

assert_equal 'Silly Tom', cat.name
assert_equal %w(tigers lions), cat.ration
assert_equal ['Chrissy', 'Missy', 'Sissy'], cat.friends.names
assert_equal 2, cat.ears
assert_equal true, cat.has_tail
assert_equal %w(one two three four), cat.paws
assert_kind_of Cheezburger, cat.cheezburger
assert_equal 2, cat.cheezburger.weight
...
puts cat.build