Generic XML importer/exporter for ruby

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

some value some other value

all you have to write is this:

require ‘xmlcodec’

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

class FirstElement < XMLCodec::XMLElement
elname ‘firstelement’
xmlsubel_mult :secondelement

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

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

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

and is LGPL licensed.


Pedro Côrte-Real.