An example of creating an XML document


    // Create the document, and the top level <windows> element.
    auto doc=x::xml::doc::create();

    auto lock=doc->writelock();

    auto windows=lock->create_child()->element({"windows"});

    for (const auto &coords:coordinates)
    {
        // Position the lock. At the end of the iteration of this loop,
        // the lock's position remains on the last created XML element, and
        // this is the simplest way to bring it back on the top level
        // <windows> element.

        lock->get_xpath("/windows")->to_node();

        // Create a child factory. Use it to create the new <window>
        // element, then it's <name> child, then it's child text element.

        auto window=lock->create_child()->element({"window"});
            ->element({"name"})->text(coords.first);

        std::ostringstream x, y, width, height;

        x << coords.second.x;
        y << coords.second.y;
        width << coords.second.width;
        height << coords.second.height;

        // At this point the creator is pointing to the text child element
        // of the <name> element. Move the creator back to the
        // <name> using parent(), use create_next_sibling() to construct
        // a factory that creates the next sibling element, then create the
        // <x> element, then its text child.
        window=window->parent()->create_next_sibling()->element({"x"})
            ->create_child()->text(x.str());

        // In the same manner create the remaining elements.
        window=window->parent()->create_next_sibling()->element({"y"})
            ->create_child()->text(y.str());
        window=window->parent()->create_next_sibling()->element({"width"})
            ->create_child()->text(width.str());
        window->parent()->create_next_sibling()->element({"height"})
            ->create_child()->text(height.str());
    }

    lock->save_file(filename);

This example creates an XML file that looks like this:

<?xml version="1.0"?>
<windows>
  <window>
    <name>dummy</name>
    <x>100</x>
    <y>200</y>
    <width>50</width>
    <height>50</height>
  </window>
  <window>
    <name>main</name>
    <x>752</x>
    <y>397</y>
    <width>416</width>
    <height>338</height>
  </window>
</windows>
	

In this example, coordinates is a map containing a std::string for a key, and a value with x, y, width and height members. This data gets written out into a simple, basic, XML file.