# Generic XML importer/exporter for ruby

**URL:** <https://www.ruby-forum.com/t/generic-xml-importer-exporter-for-ruby/60346>\
**Category:** Ruby\
**Created:** [May 12, 2006, 1:31pm UTC](https://www.ruby-forum.com/t/generic-xml-importer-exporter-for-ruby/60346 "2006-05-12T13:31:35Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pedro\_CSSSSrte-Real](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@Pedro\_CSSSSrte-Real](https://www.ruby-forum.com/u/Pedro_CSSSSrte-Real)\
**Post date:** [May 12, 2006, 1:31pm UTC](https://www.ruby-forum.com/t/generic-xml-importer-exporter-for-ruby/60346/1 "2006-05-12T13:31:35Z")

</div>

I’ve written a generic importer/exporter between XML and ruby objects.  
What this means is that to generate an importer/exporter for this  
format:

 some value some other value 

all you have to write is this:

require ‘xmlcodec’

class Root \< XMLCodec::XMLElement  
elname ‘root’  
xmlsubel :firstelement  
end

class FirstElement \< XMLCodec::XMLElement  
elname ‘firstelement’  
xmlsubel\_mult :secondelement  
end

class SecondElement \< XMLCodec::XMLElement  
elname ‘secondelement’  
elwithvalue  
xmlattr :firstattr  
end

This will create some ruby objects with methods to set the subelements  
and the attributes and to import/export XML.

Besides being able to import/export the REXML DOM it can also create  
XML text itself in one go (which is faster and uses less memory).

It also supports a “partial\_export” mode where you can export a big  
XML file by normally creating the elements but then exporting as you  
create the tree and then deleting the element so that GC can reclaim  
the object.

For the importing it has a XMLStreamObjectParser which is a stream  
parser whose events are whole elements instead of start/end tags.

I’ve created and parsed 100MB files with these API’s with minimal memory  
usage.

I’ve done this to process EAD XML files so an example of the use of  
the library is “eadcodec”, the EAD importer/exporter.

Suggestions about the code and API are very welcome.  
Opinions/Suggestions about the name “xmlcodec” would be great to.

The code can be found in

[http://www.pedrocr.net/ruby/](http://www.pedrocr.net/ruby/)

and is LGPL licensed.

Greetings,

Pedro Côrte-Real.
