Ultra-lightweight parameter acquisition tool (C++) based on tinyxml2

【Foreword】

Friends who are accustomed to using ROS will probably encounter a lot of discomfort when the engineering environment needs to be separated from ROS. The first thing to bear the brunt should be parameter acquisition. In the past, you could easily obtain the parameters in the configuration file through the parameter server, launch file, etc. provided by ROS. However, after leaving ROS, this has become a pain point. This blog will introduce an XML parameter acquisition tool written by myself!

 

【Dry goods】

Without further ado, let’s get straight to the code!

First is the header file:

#ifndef XML_PARSER_H
#define XML_PARSER_H

#include "tinyxml2.h"
#include <string>

using namespace tinyxml2;

namespace util_data
{

class SimpleParam
{
public:
  SimpleParam();
  ~SimpleParam();
  bool loadXML(const std::string xml_path);
  bool getParam(const std::string name, std::string &value);
  bool getParam(const std::string name, int &value);
  bool getParam(const std::string name, double &value);
  bool getParam(const std::string name, float &value);
  bool getParam(const std::string name, bool &value);

public:
  XMLDocument doc;
};

}

#endif

Next is the source file:

#include "xml_parser/xml_parser.h"
#include <iostream>

using namespace util_data;

SimpleParam::SimpleParam()
{}

SimpleParam::~SimpleParam()
{}

bool SimpleParam::loadXML(const std::string xml_path)
{
  if(doc.LoadFile(xml_path.c_str()) != 0)
  {
    printf( "load xml file failed \n" );
    return false;
  }
  else {
    return true;
  }
}

bool SimpleParam::getParam(const std::string name, std::string &value)
{
  XMLElement *config = doc.RootElement();
  XMLElement *param = config->FirstChildElement("param");
  while (param)
  {
    const XMLAttribute *attributeOfSurface = param->FirstAttribute();
//    std::cout << attributeOfSurface->Name() << ":" << attributeOfSurface->Value() << std::endl;
    if(name == attributeOfSurface->Value())
    {
      value = attributeOfSurface->Next()->Next()->Value();
      return true;
    }
//    while(surfaceChild)
//    {
//      content = surfaceChild->GetText();
//      surfaceChild = surfaceChild->NextSiblingElement();
//      cout << content << endl;
//    }
    param = param->NextSiblingElement();
  }
  return false;
}

bool SimpleParam::getParam(const std::string name, int &value)
{
  XMLElement *config = doc.RootElement();
  XMLElement *param = config->FirstChildElement("param");
  std::string value_str;
  while (param)
  {
    const XMLAttribute *attributeOfSurface = param->FirstAttribute();
    if(name == attributeOfSurface->Value())
    {
      value_str = attributeOfSurface->Next()->Next()->Value();
      value = atoi(value_str.c_str());
      return true;
    }

    param = param->NextSiblingElement();
  }
  return false;
}

bool SimpleParam::getParam(const std::string name, double &value)
{
  XMLElement *config = doc.RootElement();
  XMLElement *param = config->FirstChildElement("param");
  std::string value_str;
  while (param)
  {
    const XMLAttribute *attributeOfSurface = param->FirstAttribute();
    if(name == attributeOfSurface->Value())
    {
      value_str = attributeOfSurface->Next()->Next()->Value();
      value = atof(value_str.c_str());
      return true;
    }

    param = param->NextSiblingElement();
  }
  return false;
}

bool SimpleParam::getParam(const std::string name, float &value)
{
  XMLElement *config = doc.RootElement();
  XMLElement *param = config->FirstChildElement("param");
  std::string value_str;
  while (param)
  {
    const XMLAttribute *attributeOfSurface = param->FirstAttribute();
    if(name == attributeOfSurface->Value())
    {
      value_str = attributeOfSurface->Next()->Next()->Value();
      value = std::stof(value_str.c_str());
      return true;
    }

    param = param->NextSiblingElement();
  }
  return false;
}

bool SimpleParam::getParam(const std::string name, bool &value)
{
  XMLElement *config = doc.RootElement();
  XMLElement *param = config->FirstChildElement("param");
  std::string value_str;
  while (param)
  {
    const XMLAttribute *attributeOfSurface = param->FirstAttribute();
    if(name == attributeOfSurface->Value())
    {
      value_str = attributeOfSurface->Next()->Next()->Value();
      if(value_str == "true")
      {
        value = true;
      }
      else {
        value = false;
      }
      return true;
    }

    param = param->NextSiblingElement();
  }
  return false;
}

Here’s another test code:

#include <iostream>
#include "xml_parser/xml_parser.h"

using namespace std;

void example()
{
  XMLDocument doc;
  if(doc.LoadFile("/home/starlab/Box/test_ws/src/common/xml_parser/config/demo.xml")!=0)
  {
    cout << "load xml file failed" << endl;
    return;
  }

  XMLElement *scene = doc.RootElement();
  XMLElement *surface = scene->FirstChildElement("node");
  while (surface)
  {
    XMLElement *surfaceChild = surface->FirstChildElement();
    const char* content;
    const XMLAttribute *attributeOfSurface = surface->FirstAttribute();
    cout << attributeOfSurface->Name() << ":" << attributeOfSurface->Value() << endl;
    while(surfaceChild)
    {
      content = surfaceChild->GetText();
      surfaceChild = surfaceChild->NextSiblingElement();
      cout << content << endl;
    }
    surface = surface->NextSiblingElement();
  }
}

void example2()
{
  util_data::SimpleParam tool;
  std::string map_file;
  int nFeatures;
  double scaleFactor;
  bool load_map;
  if(tool.loadXML("/home/starlab/Box/test_ws/src/common/xml_parser/config/test_config.xml"))
  {
    tool.getParam("map_file", map_file);
    std::cout << map_file << std::endl;
    tool.getParam("/ORBextractor/nFeatures", nFeatures);
    std::cout << nFeatures << std::endl;
    tool.getParam("/ORBextractor/scaleFactor", scaleFactor);
    std::cout << scaleFactor << std::endl;
    tool.getParam("load_map", load_map);
    std::cout << load_map << std::endl;
  }
}

int main()
{
  example2();
  return 0;
}

【Postscript】

If this blog has been helpful to you, please give it a like! !

Guess you like

Origin blog.csdn.net/weixin_39538031/article/details/131003746
Recommended