# XMLable 0.1.1

**URL:** <https://www.ruby-forum.com/t/xmlable-0-1-1/112664>\
**Category:** Ruby\
**Created:** [August 21, 2007, 10:33pm UTC](https://www.ruby-forum.com/t/xmlable-0-1-1/112664 "2007-08-21T22:33:52Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rolando\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/d9b06d/32.png) [@Rolando\_A](https://www.ruby-forum.com/u/Rolando_A)\
**Post date:** [August 21, 2007, 10:33pm UTC](https://www.ruby-forum.com/t/xmlable-0-1-1/112664/1 "2007-08-21T22:33:52Z")

</div>

As we all need more free stuff that does the same\*, here’s another  
XML serialization library:

XMLable 0.1.1

Some copy-paste from the docs:

Simple, and I mean really-simple, non-secure way to convert ruby  
objects to/from XML data.  
(ok, right now is really simple and maybe not really non-secure, but  
it doesnâ€˜t mean it canâ€˜t be a bit more “secure” in the future :-P)  
Just make sure that your class includes the XMLable module and the  
constructor has only one parameter: a Hash with String keys for each  
attribute you want to setup on initialization.  
You can also use the utility function init\_attributes\_with\_opts to  
initialize the attributes (check the doc)  
Oh, also, XMLable depends on REXML, so make sure you require  
somewhere in your code â€˜rexml/documentâ€™.

Now, for the fun, some irb tricks:

irb(main):001:0\> require ‘xmlable’  
=\> true  
irb(main):002:0\> require ‘rexml/document’  
=\> true  
irb(main):003:0\> [1,2,3].to\_xml  
=\> … \</\>  
irb(main):004:0\> [1,2,3].to\_xml.to\_s  
=\> “12\</  
ritem\>3”  
irb(main):005:0\> [1,Time.now,nil,[“wow”,:nice]].to\_xml.to\_s  
=\> "11187725687.81283\<ritem rclass=‘NilClass’/

> wow\</  
> ritem\>nice"  
> irb(main):006:0\> data = [1,nil,“lalala”].to\_xml  
> =\> … \</\>  
> irb(main):007:0\> Array.from\_xml(data)  
> =\> [1, nil, “lalala”]  
> irb(main):008:0\> Array.from\_xml(data.to\_s)  
> =\> [1, nil, “lalala”]  
> irb(main):009:0\> data = {:hey =\> “thisisfun”, [“right”,“?”] =\> nil}  
> =\> {[“right”, “?”]=\>nil, :hey=\>“thisisfun”}  
> irb(main):010:0\> Hash.from\_xml(data.to\_xml)  
> =\> {[“right”, “?”]=\>nil, :hey=\>“thisisfun”}  
> irb(main):011:0\> module A  
> irb(main):012:1\> module B  
> irb(main):013:2\> class C  
> irb(main):014:3\> include XMLable  
> irb(main):015:3\> def initialize(opts)  
> irb(main):016:4\> init\_attributes\_with\_opts({“lala” =\> 10}, opts)  
> irb(main):017:4\> end  
> irb(main):018:3\> end  
> irb(main):019:2\> end  
> irb(main):020:1\> end  
> =\> nil  
> irb(main):021:0\> c = A:🅱:C.new({“lala” =\> 5400})  
> =\> #\<A:🅱:C:0x22ffa8 @lala=5400\>  
> irb(main):022:0\> c.to\_xml.to\_s  
> =\> “5400”  
> irb(main):023:0\> another\_c = A:🅱:C.from\_xml(c.to\_xml)  
> =\> #\<A:🅱:C:0x229298 @lala=5400\>  
> irb(main):024:0\> data = [“check”,:this,c].to\_xml.to\_s  
> =\> “checkthis5400\</  
> robject\>”  
> irb(main):025:0\> Array.from\_xml(data)  
> =\> [“check”, :this, #\<A:🅱:C:0x21d4c0 @lala=5400\>]

You can get it here (source and tests):

[http://rolando.cl/ruby/xmlable/src/xmlable.rb](http://rolando.cl/ruby/xmlable/src/xmlable.rb)  
[http://rolando.cl/ruby/xmlable/src/test.rb](http://rolando.cl/ruby/xmlable/src/test.rb)

and the doc:

[http://rolando.cl/ruby/xmlable/doc/index.html](http://rolando.cl/ruby/xmlable/doc/index.html)

- I just was lazy searching for some library, and because I was  
bored, I ended up writing my own library… 😛

Enjoy!
