Alo

What Is It?

Alo is a concise way in ANSI C code to write data in XML format to a file. 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. And RDF/OWL promise more advanced and automatic checks, searches, and connectedness to make the data more useful.

Alo Example

Pretend you want to output address book data like:

<person id="1000">
    <name>Roger Flores</name>
    <zipcode>94062</zipcode>
    <state>CA</state>
</person>

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", 
    state_abbreviations[person[index].state]);

alo_close(doc);

Notice the following points:
  1. A familar 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 familar fopen/fclose like manner.

  4. Nesting elements is simply a matter of remembering the parent element and refering 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 Mozilla Public License so you can freely use it as is without viral issues.

Tips and Tricks

  1. What's the smallest Alo example?

    
    alo_open(&doc, "small.xml", ALO_OPTION_OUTPUT_XML_DECLARATION, "");
    alo_close(doc);
    
    
    which produces a file named small.xml containing
    
    <?xml?>
    
    
  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 verbage 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 and free and fast and reasonably effective first choice. Often browsers understand and decompress gzip files. Others like Bzip2 are slower and require more memory but are even more effective. And if you want faster compression look at LZO

These have proven useful.

XML
XML is developed by the World Wide Web Consortium. It has the defining document for XML as well as several other technologies.
XML Schema
Resource Description Framework (RDF)
These W3C technologies allow better definitions of data, which are then used to reject invalid data. XML only gives syntax checking. If you believe in type checking, asserts, or checking error codes, then these are for you.
OWL Web Ontology Language
OWL is a way to describe how data relates to other data. If your XML document had a cat named FiFi and a dog named Brute you could add OWL information that “dogs chase cats”. An OWL program could reason that Brute will chase FiFi, because your XML document allows for that, and not because the OWl knows anything about chasing, let alone cats or dogs. This gets cool when a program searches thousands of OWL documents to answer your questions.
JEdit
JEdit is a nice editor for XML. It is a free Java editor that at least deserves a try.
WinMerge
WinMerge is an OK visual differ, and you need one when working on XML. It seems to be one of the better ones but could do more. My favorite visual diff tool is embedded in Metrowerk's CodeWarrior.
Subversion
I really wanted to use Arch because it supports distributed developement and has good changeset concepts. But Arch has had second class Windows support, which resulted in it simply not working. Subversion has been a pleasant surprise. It has some good design choices, supports Windows well, and it satisfies my current needs.

Send comments to Roger Flores

SourceForge.net Logo