From: Todd Burch Date: 2007-07-25T04:13:12+09:00 Subject: Re: Creating xml files: what are the right libraries to use? vasudevram wrote: > Have you thought of generating the XML yourself via puts or print or > file_or_IO_object.write method calls? Unless you're pressed for time, > or have advanced requirements like being able to handle different > encodings, this might be the easiest way - since it would not depend > on any library. One possible drawback is that the code would be a bit > less readable, with all the calls, e.g.: > > print "#{person.name}" > > and so on, if you do it that way - i.e. write out all the XML output > in 'longhand'. But what I actually mean is to do it by writing some > simple methods for each kind of tag - for opening as well as closing > XML tags, as well as for text content and attributes. Something on the > lines of: > > def start_element(element_name) > print "<" + element_name + ">" > end > > def end_element(element_name) > print "" > end > > and so on. Then use them like this: > > start_element("person") > start_element("name") > print person.name > end_element("name") > end_element("person") > > You could optionally (preferable, really) add a trailing \n to the > output of each open/close element tag, to make the output more human- > readable. > I'll be doing some XML generation soon in Ruby. I'll take a similar approach (generating it myself), but I'll use a les verbose approach, something along the lines of xmltag.push("person") xmltag.push("name") print person.name xmltag.pop xmltag.pop or xmltag.push("person", "name") print person.name xmltag.pop(2) This lets the xmltag instance methods push and pop worry about what gets opened and closed, the trailing CRLF, getting things in the right order, managing balanced tags, etc. Todd -- Posted via http://www.ruby-forum.com/.