glog与pugi::xml使用方法
(一)glog的使用:google logging的简称;
1)需要安装,网上一搜一大堆,不在赘述;
2)在cmakelists.txt中,需要链接"-glog",如:target_link_libraries(target -lpthread -lglog);
3) 测试代码如下:
#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;
}
(二)pugi::xml的使用
1)它很轻量,只有三个文件(pugiconfig.hpp pugixml.cpp pugixml.hpp ),在cmakelists.txt里面填写如下代码:
file( GLOB_RECURSE XML_SRC ${PROJECT_SOURCE_DIR}/pugixml/*.cpp)
add_executable(target main.cpp ${XML_SRC})
常用的类及定义使用方法:
pugi::xml_document doc; //定义一个xml文件类对象,准备读取文件;
pugi::xml_parse_result result; //定义一个读取xml文件的返回标志,用于是否可以正确读取文件;
pugi::xml_node node; //定义一个节点,从可以从当前节点读取;
2)xml文件的书写格式:
<?xml version = "1.0"?> <!--注释的写法格式-->
<root><user>云</user><msg>哈哈哈哈</msg>
</root>
3)包含头文件
#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;
}
输出结果:
user: 云
msg: 哈哈哈哈
4)pugi::xml中,【attribute】属性的使用方法
xml文件如下:
<?xml version = "1.0"?> <!--注释的写法格式-->
<root><bios function="suhui"> <!--属性的定义--></bios>
</root>
相关的文件解析程序案例:
pugi::xml_document doc;doc.load_file("../config/jiang.xml");cout<<doc.child("root").child("bios").attribute("function").name()<<endl; //functioncout<<doc.child("root").child("bios").attribute("function").value()<<endl; //suhui
4)pugi::xml_parse_result的使用: parse:读作怕死!!!
xml_parse_result就是load_file()成员函数返回的结果,代码如下:
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)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)pugi::xml的for循环某个节点的所有数据,代码如下:
#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用代码增加一个标签及其对应的元素,代码如下:
#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");
生成的xml文件如下:
<?xml version="1.0"?>
<root><students>刘大哥</students><teacher>张海</teacher>
</root>
<?xml version="1.0" encoding="utf-8"?>
8)pugi::xml用代码删除一个标签,[测试的xml数据是(7)生成的]代码如下:
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”); //必须保存文件 ,这样teacher标签就会被删除了!
return 0;
}