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?

Bugs & such

Please report via Github issue tracking.

Free hint: If you liek mudkipz^W^Wfeel like generous today you can tip me at tipjoy.com/u/pisuka

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 = <<-EOS
    <kitteh xmlns='urn:x-lol' xmlns:kthnx='urn:x-lol:kthnx' ears=' 2 ' kthnx:has-tail=' yes  '>
      <name xmlns='urn:x-lol:kthnx'>
          Silly
          Tom
      </name>
      <kthnx:eats>
        tigers
        lions
      </kthnx:eats>
      <pals>
        <pal>Chrissy</pal>
        <pal>Missy</pal>
        <pal>Sissy</pal>
      </pals>
      <paw>  one</paw>
      <paw> two </paw>
      <paw>three</paw>
      <paw>four</paw>
      <cheezburger weight='2' />
    </kitteh>
EOS
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