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”.
Latest Version
Alo 1.3 was released 6/15/2017.
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>
<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:
-
A familiar
printf
like formatting is used to make the output look exactly how you want it to. -
Adding an element or attribute is simple, and only takes one line. The element is added in the right spot and closed when done.
-
The output is opened and closed in a familiar
fopen
/fclose
like manner. -
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 about 7 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.
Alo has been compiled and tested on gcc 7.2, clang 4.0 and Microsoft Visual Studio Express 2017 15.5. Valgrind has been used to find and fix memory errors.
Tests
Be sure to compile and runs the tests to verify your build. They also make useful, working code examples.
# make test
# ./test
........................
passed
Tips and Tricks
-
What's the smallest Alo example?
alo_open(&doc, "small.xml", ALO_OPTION_OUTPUT_XML_DECLARATION, ""); alo_close(doc);
<?xml version="1.0" ?>
-
What's the simplest way to output an element with an attribute?
<person ID="1000">Roger Flores</person>
alo_out(doc, personN, "^e^a%u^%s", 0, "person", 0, "ID", person[index].ID, person[index].name);
-
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.
-
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.
-
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
Links
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.
- Schema.org
- This is a collection of schemas for structuring data. Use of them may make your data much more widely used.
- JEdit
- JEdit is a nice editor for XML. It is a free Java editor with numerous extensions.