mr-edd.co.uk :: horsing around with the C++ programming language

XSMELL: awesomer XML creation

[2nd July 2009]

A little library I made recently seems to be amusing people. I hadn't found time to post it here until now. From the readme:

XSMELL README

Congratulations! You have in your hands the MOST BRILLIANTEST C++ XML CREATION LIBRARY EVER CREATED.

Have you ever needed to embed a quick snippet of HTML or XML in your C++ source code? Didn't you just hate having to use that obscure string concatenation syntax?

Well no more! With the advent of XSMELL you can now use regular XML syntax directly in your source code, thanks to the reckless use of operator overloading, template meta-programming and preprocessor macros:

using namespace xsmell;

document doc =
      _
      <html>_
          <head>_
              <title>"XSMELL demo"<!title>_
          <!head>_
          <body>_
              <p>"Yesssssssssssssssss!"<!p>_
              <img .src("chucknorris.png") .alt("sneezing eyes open")>_ <!img>_
          <!body>_
      <!html>
      _;

std::cout << doc << '\n';

That's right! Thanks to XSMELL you'll no longer suffer from S-Expression envy. You've got one up on those Lisp guys now — smug bastards!

And you no longer have to worry about generating malformed XML! After spending hours fighting obscure C++ compiler errors, you'll be 100% certain that your XML is correct.

NOTE: XSMELL requires Boost headers and a modern C++ compiler with support for C99 variadic macros.

If you really must get the code, it is available on bitbucket.

Comments

Dave Abrahams

[28/09/2009 at 19:16:00]

This is awesome, right down to the choice of name.

Mateusz Loskot

[21/01/2010 at 14:37:28]

At first, brilliantly well wrapped strings making elements, attributes and values. It's enjoyable way of using C++ indeed. I like it.

On the other hand, it's just a fancy way of streaming and concatenating characters. But still, the overall idea is nice and applicable in other situations:

path p = drive("C").dir("a").dir("b").file("mama.txt");

rest r = protocol("ftp").server("a").dir("b").resource("generate").("file").("mama.txt");

And, I second Dave's comment, the name is perfect :-)

alfC

[13/03/2010 at 11:17:15]

what is next, a Latex embedder too?

using namespace ltxsmell;

document ltxdoc =
_ /section("Introduction")
_ /par("lorem ipsum")
_ /begin(equation)
_ /int("x dx = x^2")
_ /end(equation)
_ /par("lorem ipsum")
_ /includegraphics[ width = 300*pt ]("image.jpg") ;

std::cout << doc << '\n';

Up to challenge? :)

alfC

[13/03/2010 at 11:17:57]

Up to *the* challenge?

Edd

[13/03/2010 at 20:14:13]

Fortunately (for my sanity) I'm unfamiliar with LaTeX and therefore don't feel as compelled :)

However, a thought for the intrepid procrastinator: most of the underscores in the example you gave are superfluous.

alfC

[14/03/2010 at 02:20:25]

I guess all underscores but the first. Regarding your sanity, nobody believes you are sane after writing such an awesome code. Cheers!

(optional)
(optional)
(required, hint)

Links can be added like [this one -> http://www.mr-edd.co.uk], to my homepage.
Phrases and blocks of code can be enclosed in {{{triple braces}}}.
Any HTML markup will be escaped.