Creating xml files: what are the right libraries to use?

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>#{person.name}</name></person>"

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 "</" + element_name + ">"
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://jugad.livejournal.co

···

On Jul 24, 3:34 am, "Kyle Schmitt" <kyleaschm...@gmail.com> 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

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...

I use REXML in the standard lib and haven't had any problems.

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)

XML wasn't designed for that application. sorry. I would go with vasudevram's
recommendation if you must have it in XML.

- --
  Travis Warlick

  "Programming in Java is like dealing with your mom --
   it's kind, forgiving, and gently chastising.
   Programming in C++ is like dealing with a disgruntled
   girlfriend -- it's cold, unforgiving, and doesn't tell
   you what you've done wrong."

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>#{person.name}</name></person>"

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 "</" + element_name + ">"
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/\.

Humm, those are good points. Although I was under the impression that
XML was designed for some streaming capabilities in mind. I'll have
to go read some history to find that out though.

The idea of writing it myself is tempting, but it's the fact that it
sometimes ends up being hacky when you do your own xml generation.
I've done it before for other one-off tests, with varying degrees of
success.

There _may_ be a reason to do real xml out someday. As for right now,
I was just doing it as an easy to parse format :wink:

Looks like I'll just have to write up something to do this. That
should be fun :slight_smile:

--Kyle