tutorial_hello.cpp

A first program to serialize a small struct.

With this example, we see how to put data on a xml stream. We could notice the xml::content function which read only the tag content.

#include <iostream>
#include <string>
#include <ximol/ximol.hpp>

using namespace std;
using namespace ximol;

// a small struct
struct world
{
    world():x("world"){};
    string x;
};

// struct serialization
xostream& operator<<(xostream& xos, const world & w)
{
    return xos << xml::stag("world") 
               << xml::content(w.x)
               << xml::etag();
};

xistream& operator>>(xistream& xis, world & w)
{
    return xis >> xml::stag("world") 
               >> xml::content(w.x)
               >> xml::etag();
};

// an other struct
struct hello
{
    hello():k(1),x(){};
    world x;
    int k;
};

// struct serialization
xostream& operator<<(xostream& xos, const hello & h)
{
    return xos << xml::stag("hello") 
               << h.x
               << xml::box("k",xml::content(h.k))
               << xml::etag();
};

xistream& operator>>(xistream& xis, hello & h)
{
    return xis >> xml::stag("hello") 
               >> h.x
               >> xml::box("k",xml::content(h.k))
               >> xml::etag();
};

// main function
int main()
{
    xstringstream xss;
    hello h; 

    xss << xml::prolog() << h;

    cout << str<string>::cast(xss.str()) << endl;

    hello h2;
    h2.k=3; h2.x.x="foo";

    xss >> xml::prolog() >> h2;

    cout << "h2.k=" << h2.k << endl;
    cout << "h2.x.x=" << h2.x.x << endl;
    return 0;
};


Donate to the XiMoL project SourceForge.net Logo If you have any questions about XiMoL, you could write to tournois@users.sourceforge.net.