From: Miles Monroe Date: 2006-03-14T05:35:35+09:00 Subject: Re: Wrapping XML document in Class Miles Monroe wrote: > I would like to use Ruby and REXML to wrap an XML document and allow > access to the element nodes, attributes, and text. Essentially I need > an XML document builder loosely coupled from the document. > > It seems like Ruby is powerful enough to code generate a class given an > XML document or build an object at run-time with the appropriate > accessors. > > For instance: > > order = XmlBuilder.new > > order.add("root").add("header") > order.root.add("body") > > order.body.add("component").attributes["type"] = "metric" > > order.write($stdout) > > I'd also like to be able to plug-in input validation when assigning > values to elements and attributes. > > Has anyone done this before? > > Thanks. I found this blog entry on a XmlStringBuffer class in Ruby. I understand using method_missing to create the element, but I'm not sure how to closures would be implemented. Thanks. http://www.beust.com/weblog/archives/000025.html xml = XML.new xml.html { xml.head { } xml.body { xml.table { xml.tr { xml.td({ "valign" => "top"}, "Content1"){ } xml.td { xml.append("Content2") } } } } } -- Posted via http://www.ruby-forum.com/.