basic-xml 1.0.1
Very basic loader/writer of XML tags with attributes and content.
basic-xml

The Basic XML library and tools are used to handle very basic XML files. At the moment, the library can load a file composed of XML tags with attributes and text. It does not support anything more. This is primarily useful for simple tree based files defining settings.

You can use this library to read simple XML files. Just define an XML object and get the root to go through the tree of nodes.

basic_xml::xml x(filename);
basic_xml::node::pointer_t root(x.root());
...

The nodes have the following basic functions:

  • node::tag_name() – returns the name of the tag, it has to be a valid token
  • node::text() – returns the text found between the start and end tags
  • node::attribute() – retrieve the named attribute
  • node::first_child() – get the first child of that node
  • node::next() – get the next node from this node

Other functions are available but the ones above should generally be sufficient to go through all the nodes as required by your application.

The project includes the basic-xml tool which can be used to reformat an XML file in one long line, extract a value, or lint a file.

The library allows you to specify a C++ stream as input. This allows you to read from stdin:

basic_xml::xml x("stdin", std::cin);
...

The library can be used to safely create an XML file (i.e. making sure that closing tags will always be present, that tag names are valid tokens, that attributes are defined as expected, etc.) For that purpose, create a root node and then print the result once done.

basic_xml::node::pointer_t root(std::make_shared<basic_xml::node>("simple"));
basic_xml::node::pointer_t node_one(std::make_shared<basic_xml::node>("one"));
one.set_text("some data here");
root.append_child(node_one);
std::cout << *root << "\n";

Note that the library does not allow for deleting tags. In other words, you need to create the correct file from the start.

Warning
The library reads the entire file in memory. It is not expected to be used to stream XML data.

This document is part of the Snap! Websites Project.

Copyright by Made to Order Software Corp.