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

K

Kyle Schmitt

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
 
V

vasudevram

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://www.dancingbison.com
http://jugad.livejournal.co
http://sourceforge.net/projects/xtopdf
 
T

Travis D Warlick Jr

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Kyle said:
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."
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFGpk5EWvapaOIz2YYRAkSGAJ0aEOMiKzMHD1n8nrn9Z4DKOOdDIACdHarL
He/7nlkeAJ/IIpKeOLXebOc=
=Jqq1
-----END PGP SIGNATURE-----
 
T

Todd Burch

vasudevram said:
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
 
K

Kyle Schmitt

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 :)

--Kyle
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top