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

**URL:** <https://www.ruby-forum.com/t/creating-xml-files-what-are-the-right-libraries-to-use/110186>\
**Category:** Ruby\
**Created:** [July 24, 2007, 12:35am UTC](https://www.ruby-forum.com/t/creating-xml-files-what-are-the-right-libraries-to-use/110186 "2007-07-24T00:35:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kyle\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f6c823/32.png) [@kyle\_s](https://www.ruby-forum.com/u/kyle_s)\
**Post date:** [July 24, 2007, 12:35am UTC](https://www.ruby-forum.com/t/creating-xml-files-what-are-the-right-libraries-to-use/110186/1 "2007-07-24T00:35:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kyle\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f6c823/32.png) [@kyle\_s](https://www.ruby-forum.com/u/kyle_s)\
**Post date:** [July 24, 2007, 9:01pm UTC](https://www.ruby-forum.com/t/creating-xml-files-what-are-the-right-libraries-to-use/110186/2 "2007-07-24T21:01:44Z")

</div>

On Jul 24, 3:34 am, “Kyle S.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> 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 “\</” + 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

> **[dancingbison.com](http://ww38.dancingbison.com/)**
>
> Learn Python Programming

[http://jugad.livejournal.co](http://jugad.livejournal.co)

> **[Conversion of other file formats to PDF](https://sourceforge.net/projects/xtopdf/)**
>
> Download Conversion of other file formats to PDF for free. xtopdf: Tools to convert other formats (x) to PDF; x as in math.

---

<div class="post-metadata">

**Author:** ![kyle\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f6c823/32.png) [@kyle\_s](https://www.ruby-forum.com/u/kyle_s)\
**Post date:** [July 24, 2007, 9:11pm UTC](https://www.ruby-forum.com/t/creating-xml-files-what-are-the-right-libraries-to-use/110186/3 "2007-07-24T21:11:32Z")

</div>

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

Kyle S. 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 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](http://enigmail.mozdev.org)

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

---

<div class="post-metadata">

**Author:** ![kyle\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f6c823/32.png) [@kyle\_s](https://www.ruby-forum.com/u/kyle_s)\
**Post date:** [July 24, 2007, 9:31pm UTC](https://www.ruby-forum.com/t/creating-xml-files-what-are-the-right-libraries-to-use/110186/4 "2007-07-24T21:31:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kyle\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f6c823/32.png) [@kyle\_s](https://www.ruby-forum.com/u/kyle_s)\
**Post date:** [July 24, 2007, 9:13pm UTC](https://www.ruby-forum.com/t/creating-xml-files-what-are-the-right-libraries-to-use/110186/5 "2007-07-24T21:13:04Z")

</div>

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 “\</” + 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
