genx
a safe XML generator for Lua

Installing

You can install genx with LuaRocks:

$ luarocks install http://genx.luaforge.net/genx-0.5.0-1.rockspec

Example

Here's a contrived example that writes an XML document to the standard output.
    require 'genx'
    d = genx.new(io.stdout)
    
    d:startElement('farm')

    d:startElement('crop')
    d:attribute('type', 'fruit')
    d:text('Apricots')
    d:endElement()

    d:startElement('crop')
    d:attribute('type', 'vegetable')
    d:text('Zucchini')
    d:endElement()

    d:endElement()
This program outputs the following:
<farm><crop type="fruit">Apricots</crop><crop type="vegetable">Zucchini</crop></farm>

Files and Senders

There are two ways of initializing XML documents in genx; they direct their output differently. In the first, the XML output is just sent to an open, writable filehandle. This is used in the above example: io.stdout is the destination file. The usual thing is to do something like this:
doc = genx.new(io.open('out.xml', 'w'))
The second method is to use callback functions to do whatever you please with the output. You can define a send method, which handles new output, and (optionally) a flush method, to flush any buffers you may keep around. If you're writing XML over a network, for example, you might do something like this:
function send(text)
    someSocket:write(text)
end
doc = genx.new(send)
If you need to use a flush method, make it the second argument to new. It should take no arguments.

API Reference

Function-level LuaDoc documentation is available for more specifics on the functions provided by this Lua library. If you need even more detail, you may be interested in reading the original documentation for the Genx C library.