How to use glog and pugi::xml

(1) The use of glog: the abbreviation of google logging;
1) It needs to be installed, there are a lot of them online, so I won’t go into details;
2) In cmakelists.txt, you need to link "-glog", such as: target_link_libraries(target -lpthread - lglog);
3) The test code is as follows:

#include<iostream>
#include <glog/logging.h>  // glog头文件

using namespace std;

int main(void)
{
    
    
    FLAGS_log_dir = "/home/jiang/Desktop/test/log"; //路径必须存在,/在InitGoogleLogging()前设置
    FLAGS_logtostderr = false;  //TRUE:标准输出,FALSE:文件输出
    FLAGS_colorlogtostderr = true; //标准输出带颜色
    google::InitGoogleLogging("target"); //必须初始化
    //SetStderrLogging语句是设置打印输出等级,默认是ERROR,如果是FATAL,则只有FATAL打印;
    google::SetStderrLogging(google::INFO);
    LOG(INFO) << "姜怀伟的日志文件---------";
    LOG(WARNING)<<"warnning--------------";
    LOG(ERROR)<<"Error-------------------";
    //上面三句话会在"/home/jiang/Desktop/test/log"目录下生成3个文件及相关的软链接;
    google::ShutdownGoogleLogging(); //当要结束glog时必须关闭库,否则会内存溢出
    return 0;
}

(2) Use of pugi::xml
1) It is very lightweight, with only three files (pugiconfig.hpp pugixml.cpp pugixml.hpp). Fill in the following code in cmakelists.txt:
file( GLOB_RECURSE XML_SRC ${PROJECT_SOURCE_DIR}/ pugixml/*.cpp)
add_executable(target main.cpp ${XML_SRC})

Commonly used classes and definition usage:
pugi::xml_document doc; //Define an xml file class object and prepare to read the file;
pugi::xml_parse_result result; //Define a return flag for reading the xml file, used to determine whether it is possible Read the file correctly;
pugi::xml_node node; //Define a node that can be read from the current node;

2) Writing format of xml file:

<?xml version = "1.0"?>   <!--注释的写法格式-->
<root>
  <user></user>
  <msg>哈哈哈哈</msg>
</root>

3) Include header files

#include <iostream>
#include "pugiconfig.hpp"
#include "pugixml.hpp"
#include <string>
using namespace std;

int main()
{
    
    
	pugi::xml_document doc;
	doc.load_file("../config/jiang.xml");
	pugi::xml_node response = doc.child("root");
	pugi::xml_node sn = response.child("user");
	cout << "user: " << sn.child_value() << endl;
	pugi::xml_node node = response.child("msg");
	cout << "msg: " << node.child_value() << endl;    
	return 0;
}

Output result:

user: 云
msg: 哈哈哈哈

4) In pugi::xml, the usage method of [attribute] attribute
xml file is as follows:

<?xml version = "1.0"?>   <!--注释的写法格式-->
<root>
  <bios function="suhui"> <!--属性的定义-->
  </bios>
</root>

Related file parsing program cases:

    pugi::xml_document doc;
    doc.load_file("../config/jiang.xml");
    cout<<doc.child("root").child("bios").attribute("function").name()<<endl;  //function
    cout<<doc.child("root").child("bios").attribute("function").value()<<endl;  //suhui

4) Use of pugi::xml_parse_result: parse: pronounced as fear of death! ! !
xml_parse_result is the result returned by the load_file() member function. The code is as follows:

int readXML(const char* xmlName)
{
    
    
    pugi::xml_document doc;
    pugi::xml_parse_result result = doc.load_file(xmlName);
    if (result.status == 0) 
    {
    
    
        cout << "加载成功 " << endl;
    }
    else
    {
    
    
        cout << " 加载xml失败 " << xmlName << endl;
        return -1;
    }
    return 0;
}

int main(void)
{
    
    
    readXML("../config/config_xiaoche.xml");
	return 0;
}

5) How to use pugi::xml_node:

int main(void)
{
    
    
    pugi::xml_document doc;
    doc.load_file("../config/config_xiaoche.xml");
    cout<<doc.child("root").child("IVSIGNAL").child("traffic_sign").child_value()<<endl;  
    //定义一个节点,这样就可以直接使用节点类;
    pugi::xml_node sig = doc.child("root").child("IVSIGNAL").child("traffic_sign");
    cout<<sig.child_value()<<endl;
	return 0;
}

6) The for loop of pugi::xml loops all the data of a node. The code is as follows:

#include <iostream>
#include "pugiconfig.hpp"
#include "pugixml.hpp"
#include <string>
using namespace std;
/*测试文件*/
/*
<?xml version="1.0"?>
<root>
  <IVSIGNAL>
    <steer_angle_error>0</steer_angle_error>
    <camera_angle_error>0</camera_angle_error>
    <traffic_sign>1</traffic_sign>
  </IVSIGNAL>
</root>
*/
int main(void)
{
    
    
    pugi::xml_document doc;
    doc.load_file("../config/config_xiaoche.xml");
    pugi::xml_node ivsignal = doc.child("root").child("IVSIGNAL");
    for(pugi::xml_node input = ivsignal.first_child(); input ;input = input.next_sibling())  //xml遍历某个节点下的数据;
    {
    
    
        cout<<input.child_value()<<' ';
    }
	return 0;
}
// 输出结果: 0 0 1

7) pugi::xml uses code to add a tag and its corresponding element. The code is as follows:

  #include <iostream>
#include <cstdint>
#include "pugixml.hpp"
#include <stdio.h>

pugi::xml_document xmlDoc;
pugi::xml_node nodeRoot = xmlDoc.append_child("root");
// 声明
pugi::xml_node pre = xmlDoc.append_child(pugi::node_declaration);
pre.append_attribute("version") = "1.0";
pre.append_attribute("encoding") = "utf-8";

pugi::xml_node nodeStudents = nodeRoot.append_child("students");
nodeStudents.append_child(pugi::node_pcdata).set_value("刘大哥");

nodeStudents = nodeRoot.append_child("teacher");
nodeStudents.append_child(pugi::node_pcdata).set_value("张海");
xmlDoc.save_file("test.xml");

The generated xml file is as follows:

<?xml version="1.0"?>
<root>
	<students>刘大哥</students>
	<teacher>张海</teacher>
</root>
<?xml version="1.0" encoding="utf-8"?>

8) pugi::xml uses code to delete a tag. [The tested xml data is generated by (7)] The code is as follows:
int main()
{ pugi::xml_document xmlDoc; if(xmlDoc.load_file("bbbb.xml") ) { pugi::xml_node node = xmlDoc.child(“root”); cout<<node.child_value(“teacher”)<<endl; node.remove_child(“teacher”); } xmlDoc.save_file(“test.xml "); //The file must be saved so that the teacher tag will be deleted! return 0; }









Guess you like

Origin blog.csdn.net/qq_30143193/article/details/132849217