What Is It?

If you need to write out XML documents and already know C's printf, you can quickly use Alo. Most other solutions create a DOM, format elements and add them to the DOM, then write out the DOM, or they sprinkle printf calls all over. The Alo API handles XML structure, XML syntax, and data formatting so your code can focus on what data to output. Alo is pronounced like “Aloe”.

Why XML?

There are many better places to read arguments for and against XML. We like how a text editor allows the data to be inspected and modified. And a visual diff can compare two files. Data format changes can be made without much work, and compatibility can be preserved with little effort. XMLSchema can be used to validate the data with tools. CSS can be used to nicely display some data formats. JSON can be a good alternative for just data.

Alo Example

Pretend you want to output address book data like:

<person id="1000">
    <name>Roger Flores</name>

You could write C code using Alo like this:

rootN = alo_open(&doc, "person.xml", ALO_OPTION_NONE, "");

personN = alo_out(doc, rootN, "^e", 0, "person");
alo_out(doc, personN, "^a%u", 0, "id", person[index].id);
alo_out(doc, personN, "^e%s", 0, "name", person[index].name);
alo_out(doc, personN, "^e%u", 0, "zipcode", person[index].zipcode);
alo_out(doc, personN, "^e%s", 0, "state", 


Notice the following points:
  1. A familiar printf like formatting is used to make the output look exactly how you want it to.

  2. Adding an element or attribute is simple, and only takes one line. The element is added in the right spot and closed when done.

  3. The output is opened and closed in a familiar fopen/fclose like manner.

  4. Nesting elements is simply a matter of remembering the parent element and referring to it when the child is outputted.

A tutorial is available to quickly get started. A complete list of commands to write XML is in the documentation, also included in the distribution.

Alo is a few KB compiled and can be easily added to your program. It is great for small or embedded projects.

Alo is licensed under the GNU Lesser General Public License, just like the commonly used glibc.

Tips and Tricks

  1. What's the smallest Alo example?

    alo_open(&doc, "small.xml", ALO_OPTION_OUTPUT_XML_DECLARATION, "");
    which produces a file named small.xml containing
    <?xml version="1.0" ?>
  2. What's the simplest way to output an element with an attribute?

    <person ID="1000">Roger Flores</person>
    can be written by
    alo_out(doc, personN, "^e^a%u^%s", 0, "person", 0, "ID", 
            person[index].ID, person[index].name);
  3. Doesn't XML treat characters like '<', '>' and '&' specially? What's an easy way to handle it if my data has them?

    Alo handles those characters and others for you. Just give it the data and don't worry. Alo is designed to only output valid XML.

  4. How should I output large binary blobs of data?

    Either embed them or output a link to the data elsewhere. Embed data by encoding it. Try base64 or uuencode. When linking data, store the data elsewhere like on a web site, ftp site, or local filesystem. Or consider packaging the data with the xml and sending them together. Something like multipart MIME, tar or zip should work.

  5. My XML data is large? How do I shrink it?

    Consider rewriting the XML. Nesting can reduce verbiage at the cost of flexibility. Shorter element and attribute names can help, but don't chose cryptic replacements. A better way is to compress the data. Gzip is a simple, free, fast and reasonably effective first choice plus browsers understand and decompress gzip files. If you want faster compression for your program look at LZO

These have proven useful.

XML is developed by the World Wide Web Consortium. It has the defining document for XML as well as several other technologies.
This is a collection of schemas for structuring data. Use of them may make your data much more widely used.
JEdit is a nice editor for XML. It is a free Java editor with numerous extensions.

Send comments to Roger Flores Logo