• glog与pugi::xml使用方法


    (一)glog的使用:google logging的简称;
    1)需要安装,网上一搜一大堆,不在赘述;
    2)在cmakelists.txt中,需要链接"-glog",如:target_link_libraries(target -lpthread -lglog);
    3) 测试代码如下:

    #include
    #include   // 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;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    (二)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>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    3)包含头文件

    #include 
    #include "pugiconfig.hpp"
    #include "pugixml.hpp"
    #include 
    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;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    输出结果:

    user: 云
    msg: 哈哈哈哈
    
    • 1
    • 2

    4)pugi::xml中,【attribute】属性的使用方法
    xml文件如下:

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

    相关的文件解析程序案例:

        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
    
    • 1
    • 2
    • 3
    • 4

    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;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    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;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    6)pugi::xml的for循环某个节点的所有数据,代码如下:

    #include 
    #include "pugiconfig.hpp"
    #include "pugixml.hpp"
    #include 
    using namespace std;
    /*测试文件*/
    /*
    
    
      
        0
        0
        1
      
    
    */
    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
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    7)pugi::xml用代码增加一个标签及其对应的元素,代码如下:

      #include 
    #include 
    #include "pugixml.hpp"
    #include 
    
    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");
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    生成的xml文件如下:

    <?xml version="1.0"?>
    <root>
    	<students>刘大哥</students>
    	<teacher>张海</teacher>
    </root>
    <?xml version="1.0" encoding="utf-8"?>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    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.remove_child(“teacher”);
    }
    xmlDoc.save_file(“test.xml”); //必须保存文件 ,这样teacher标签就会被删除了!
    return 0;
    }

  • 相关阅读:
    免费:实时 AI 编程助手 Amazon CodeWhisperer
    Java EE进阶2
    磷酸酶、转录因子、KRAS ——“不可成药”靶点?
    python super用法
    Java文件流练习
    如何公网远程访问OpenWRT软路由web界面
    郑州轻工业大学OJ合集(C语言)【正在整理】
    Spring Boot 和 Spring Framework 的区别是什么?
    Python例题练习1
    云计算高级课程作业
  • 原文地址:https://blog.csdn.net/qq_30143193/article/details/132849217