Valid, literal XML in C++ with Blasien

2015-07-05

Creating and processing XML feels awkward in most programming languages. With Blasien, a tiny C++11 header library, XML in C++ feels easy and natural. As an extra the XML that is written is mostly validated at compile time.

Here is an example:

XHTML

C++ with Blasien

The same syntax can be used to create a DOM.

Background

Code to create XML is usually a matter of calling functions like startElement, setAttribute, endElement etc. Such code looks nothing like the desired XML. And there is no static type checking. Here is a typical example:

XHTML

C++

This code looks unpleasant and it is easy to make errors. The tag names are written as string: a typo there can go undetected for a long time.

Elements are closed with writeEndElement(). Matching up the opening and closing of tags is hard to do visually and errors there are not caught at compile time.

There are programming languages, like XSLT and XQuery, that work better with XML. Calling code in these languages from C++ is inconvenient and requires that the programmer learns an additional programming language.

A few years ago, I created a way to work with XML from C++. In that way, wrapper classes were created for each element type from a schema definition. This prevented many possible errors at compile time. But the code still did not look like XML. Blasien has all the same checks but with a nicer syntax:

C++ with writeodf

C++ with Blasien

Secret sauce 1: operator< and operator>

Blasien is built on a powerful C++ feature: operator overloading. Nearly all operators can be overloaded in C++. For XML, two operators are most distinctive: < and >. These operators usually mean “smaller than” and “larger than” and are used in expressions like if (x > 3) { ... }. There is no rule that limits the use of < and > to mathematical expressions. As you will see, they can be put to very different use.

The operators < and > are left-associative. The left-most combination of expression, operator, expression is replaced first. The left-most expression is a sink for XML expressions. Each handled expression leads to a new sink with a different state.

on GitHub for now. Feel free to file issues or send pull requests.

The code is currently under LGPL3, but I’m open to additional licenses if a project requires it.

Comments

Post a comment