C++ XML Reader

After stumbling upon a need for multiple configurations for my hobby C++ forex trading bot development, I’ve decided to implement simple xml config files.

As of now, I did not need anything fancy: just the ability to read simple xml format with login and password settings (with the possibility to extend it further with configurable settings for traded currencies):

<config>
  <credentials>
    <login>MyLogin</login>
    <password>MyPassword</password>
  </credentials>
</config>

The pugixml library has really been a pleasant surprise with that: having only three source files (two of which are headers), it can easily be compiled as a static lib or simply included into the project. As for reading the above values, it is really simple:

   
    pugi::xml_parse_result result = doc.load_file(filename);
    if (result)
    {
        is_loaded = true;
        pugi::xml_node root = doc.child("config");

        username = root.child("credentials").child_value("login");
        password = root.child("credentials").child_value("password");
    }
    else
    {
        printf("Error while loading file: %s\n", result.description());
    }

Overall, I’ll continue to use this solution.

Leave a Reply

Your email address will not be published. Required fields are marked *