当前位置: 首页 > news >正文

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;
}

http://www.lryc.cn/news/170765.html

相关文章:

  • windows下MySQL服务不见,服务无法启动,服务闪退,提示“本地计算机上的MySQL服务启动后停止。某些服务在未由其他服务或程序使用时将自动停止”
  • 剑指offer(C++)-JZ67:把字符串转换成整数atoi(算法-模拟)
  • 嵌入式笔试面试刷题(day15)
  • 【Docker】Dockerfile构建镜像
  • fota升级,可卸载apk也进行更新
  • ASP.NET dotnet 3.5 实验室信息管理系统LIMS源码
  • 2023!6招玩转 Appium 自动化测试
  • WireShark抓包分析TCP三次握手过程,TCP报文解析
  • 【C语言】指针和数组笔试题解析
  • Vue的模板语法(下)
  • Zookeeper客户端——I0Itec-zkClient
  • 火山引擎 ByteHouse:ClickHouse 如何保证海量数据一致性
  • hashmap使用
  • Centos7配置国内yum源
  • C#中async/await的线程ID变化情况
  • 网络安全—黑客技术—自学笔记
  • 功夫再高也怕菜刀。多年经验,会独立开发的机器视觉工程师,技术太强,但是找工作能力差劲
  • numpy的多项式函数: `poly1d`
  • Python灰帽编程——错误异常处理和面向对象
  • 【20230919】win11无法删除Chrome注册表项
  • TCP/IP客户端和服务器端建立通信过程
  • Python ---使用Fake库向clickhouse造数据小案例
  • 09MyBatisX插件
  • 使用 Messenger 跨进程通信
  • Spring Cloud Gateway
  • JVM 优化技术
  • 【MySQL系列】- MySQL自动备份详解
  • 指针笔试题讲解-----让指针简单易懂(2)
  • 使用windbg分析dump文件的方法
  • 【论文阅读 07】Anomaly region detection and localization in metal surface inspection