From: Chris Morris Date: 2002-03-18T05:20:56+09:00 Subject: [ANN] Xml Serialization for Ruby =Xml Serialization for Ruby Download 1.0.pre1:: http://prdownloads.sourceforge.net/scrapware/clxmlserial.1.0.pre1.zip REXML (>=1.2.5):: http://www.germane-software.com/~ser/Software/rexml Home Page:: http://clabs.org/clxmlserial.htm ===Overview Xml Serialization allows classes to be marshalled to and from XML, with an emphasis on concise, human readable and editable XML. It consists of a module (+XmlSerialization+) and modified standard classes which add +to_xml+ and +from_xml+ methods. +to_xml+ is an instance method and returns an XML element containing the data from each instance variable in the including class. +from_xml+ is a singleton/class method which accepts an XML element and creates an instance of the class with the data in the element. Currently, REXML is used for XML parsing. In later versions, I hope to refactor out the dependency on REXML so other XML processors could be plugged-in. This is still very much in a pre-release state, though functional. If your interested in this project, please feel free to offer up feedback. ===License Copyright (c) 2002, Chris Morris (mailto:chrismo@homemail.com). BSD license. ===Install % ruby install.rb ===Usage See the examples directory for a sample. Unit tests are also included in clxmlserialtest.rb. Here's a quick sample: require 'cl/xmls' class MyClass include XmlSerialization attr_accessor :attr end doc = REXML::Document.new(File.open("class.xml")) c = MyClass.from_xml(doc.root) c.attr = 'new value' f = File.new("class.xml", File::CREAT|File::TRUNC|File::RDWR) c.to_xml.write(f, -1) f.close yields either: new value or: new value The +XmlSerialization+ module includes a singleton configuration class with an +outputSimpleTypeElements+ setting. Turning this off gives more concise XML, but currently loses type information. For example, a Fixnum type will be loaded as a String. Currently, the following standard classes are supported: * +String+ * +Fixnum+ * +Array+ * +Hash+ * +Time+ * (+Integer+, +Float+ and +Bignum+ should be added by next release) +Array+ and +Hash+ work fine, even nested arrays and hashes, although the XML gets a little out of hand in the concise department. Later releases should tighten this up.