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)
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 “</” + element_name + “>”
end
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.
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 W.
“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.”
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
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
Looks like I’ll just have to write up something to do this. That
should be fun
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 “</” + element_name + “>”
end
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
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
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.