From: vasudevram Date: 2007-07-25T03:59:59+09:00 Subject: Re: Creating xml files: what are the right libraries to use? On Jul 24, 3:34 am, "Kyle Schmitt" wrote: > When creating an xml document with ruby, what are currently the > "right" libraries to use. > I ask because there seem to be dozens in various states of > completeness and maintenance... > > In this case I'd use to to create a large xml file, a little bit at a > time (so I'd really rather it wasn't holding the whole blasted thing > in memory) > > Thanks, > Kyle 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. If you really have special requirements, one idea is to check out the book "Enterprise Integration with Ruby" from the Pragmatic Bookshelf, which has some discussion on doing it both ways - rolling your own as well as using some simple libraries. Vasudev Ram http://www.dancingbison.com http://jugad.livejournal.co http://sourceforge.net/projects/xtopdf