0.0
No commit activity in last 3 years
No release in over 3 years
Create object trees out of XML documents
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 0
>= 0

Runtime

 Project Readme

xml_resource

Turn XML into Ruby objects

Install

In your Gemfile:

gem 'xml_resource', '~> 3.0.0'

Use

xml = %q{
<library>
  <books>
    <book>
      <name>The Tempest</name>
      <author>William Shakespeare</author>
      <firstpublished>Nov 1, 1611</firstpublished>
    </book>
    <book>
      <name>Moby-Dick</name>
      <author>Herman Melville</author>
      <firstpublished>Oct 18, 1851</firstpublished>
    </book>
  </books>
  <hours>
    <from>08:00 AM</from>
    <till>06:00 PM</till>
  </hours>
</library>
}

class Library
  include XmlResource::Model
  
  has_collection :books

  has_attribute :open_from, :xpath => 'hours/from'
  has_attribute :open_till, :xpath => 'hours/till'
end

class Book
  include XmlResource::Model
  
  has_attribute :name
  has_attribute :author
  has_attribute :published, :type => :date, :xpath => 'firstpublished'
end

library = Library.from_xml(xml)
=> #<Library:0x00000100dcf7e8 @attributes={"open_from"=>"08:00 AM", "open_till"=>"06:00 PM"}, @books=[#<Book:0x00000100dd1778 @attributes={"name"=>"The Tempest", "author"=>"William Shakespeare", "published"=>Tue, 01 Nov 1611}>, #<Book:0x00000100dcfbd0 @attributes={"name"=>"Moby-Dick", "author"=>"Herman Melville", "published"=>Sat, 18 Oct 1851}>]>

library.open_from
=> "08:00 AM"

book = library.books.first
=> #<Book:0x00000100dd1778 @attributes={"name"=>"The Tempest", "author"=>"William Shakespeare", "published"=>Tue, 01 Nov 1611}>

book.published
=> Tue, 01 Nov 1611