Dan Callaghan

Generating XML in Vala with Genx

I needed to programmatically generate some XML in a Vala project I’m working on, and I wanted to use Tim Bray’s Genx to do it. (At first I tried using libxml’s xmlwriter module but its namespace handling was inadequate, and it just made things generally unpleasant.)

The Genx API is really rather nice, and for the most part was well-suited to binding with Vala. Genx has a “sender” abstraction for the destination of its XML output, so I wrote a simple implementation that sends output to a GString (StringBuilder in Vala).

Unfortunately the API is also splattered with status return codes; necessary in a plain C API, but with GLib using GError (exceptions in Vala) would be nicer. In my case however, my code didn’t need to handle any failure cases: any status other than success indicates a bug. So I wrote a bunch of thin wrappers around the Genx functions which just assert a successful status using g_return_if_fail and family.

I’m releasing the wrapping code and .vapi file into the public domain, in case they are useful to someone else:

Place genx.vapi in your project’s vapi directory (--vapidir option to valac), ensure genx-glib.h is on your C compiler’s include search path, and link genx-glib.c into your executable.

Originally published . Tagged: , , .