yaml-cpp生成yaml文件及解析yaml文件
1) 源码编译及安装
获取源码
$ git clone https://github.com/jbeder/yaml-cpp.git
$ cd yaml-cpp && mkdir build && cd build && cmake .. && make && make install
使用样例:
由于yaml格式文件与xml和json格式的文件类似,采用树形结构。Yaml对于树节点定义为Node。Node有以下几种type概念:
Null 空节点
Sequence 序列,类似于一个Vector,对应YAML格式中的数组
Map 类似标准库中的Map,对应YAML格式中的对象
Scalar 标量,对应YAML格式中的常量
yaml格式文件MLServerConf.yaml
# 云服务器自学习配置 MLServer: MultiAddr: - ip: 192.168.2.1 mac: 10:10:56:c0:00:01 - ip: 172.16.224.1 mac: 20:10:56:c0:00:08 Version: 3.1.3 Nodes: Midware: enable: 1 Uuid: 3f79ffdd-238c CpuModel: Core(TM) Trans: enable: 1 Uuid: 58f89655-f790 CpuModel: Xron(R)
生成上述MLServerConf.yaml文件源码:
/* * yaml-cpp-test.cpp * * Created on: Aug 9, 2023 * Author: Jule */ #include <iostream> #include <assert.h> #include <fstream> #include <yaml-cpp/yaml.h> using namespace std; #define MLSERVERCONF "./ML_Alg.yaml" int main(void) { // 1. 创建yaml YAML::Node ipInfo1; ipInfo1["ip"] = "192.168.2.1"; ipInfo1["mac"] = "10:10:56:c0:00:01"; YAML::Node ipInfo2; ipInfo2["ip"] = "172.16.224.1"; ipInfo2["mac"] = "20:10:56:c0:00:08"; YAML::Node MultiAddr; MultiAddr["MultiAddr"].push_back(ipInfo1); MultiAddr["MultiAddr"].push_back(ipInfo2); YAML::Node Node1; Node1["enable"] = 1; Node1["Uuid"] = "3f79ffdd-238c"; Node1["CpuModel"] = "Core(TM)"; YAML::Node Node2; Node2["enable"] = 1; Node2["Uuid"] = "58f89655-f790"; Node2["CpuModel"] = "Xron(R)"; YAML::Node Nodes; Nodes["Midware"] = Node1; Nodes["Trans"] = Node2; YAML::Node rootnode; assert(rootnode.IsNull()); // 初始化的节点是Null类型 // 1. 创建节点 rootnode["MLServer"] = MultiAddr; // 将MultiAddr作为rootnode的一个子项 rootnode["MLServer"]["Version"] = "3.1.3"; //node.force_insert("MLServer", "nodes");//这个操作和上面等价,但是它不会检查是否存在"key"键,不推荐使用 rootnode["MLServer"]["Nodes"] = Nodes; // rootnode.remove(Nodes);//你可以通过指定一个node来删除它 // rootnode.remove("Version");//你也可以通过指定key来删除它 //rootnode << "comment: keep something usefull. #注释保留"; // 终端输出。 //std::cout << rootnode << endl; // 保存为yaml格式文件。 std::ofstream file(MLSERVERCONF); file << rootnode <<std::endl; file.close(); // 2. 解析 YAML::Node mlserver_node; try{ mlserver_node = YAML::LoadFile(MLSERVERCONF); // cout << mlserver_node << endl; }catch(YAML::BadFile &e){ perror("YAML::Load(MLSERVERCONF) failed.\n"); return 1; } if(!mlserver_node["MLServer"] || !mlserver_node["MLServer"].IsMap()) { printf("no MLServer\n"); return 2; } YAML::Node ml_root_node = mlserver_node["MLServer"]; for(YAML::const_iterator iter = ml_root_node.begin(); iter != ml_root_node.end(); ++iter) { string ChildName = iter->first.as<string>(); printf("ChildName:%s\n", ChildName.c_str()); // MultiAddr节点解析 if(0 == ChildName.compare("MultiAddr") && iter->second.IsSequence()) { for(int i = 0; i < iter->second.size(); ++i) { if(iter->second[0]["ip"] && iter->second[0]["ip"].IsScalar()) { string str_ip = iter->second[0]["ip"].as<string>(); printf("str_ip:%s\n", str_ip.c_str()); } } }else if(0 == ChildName.compare("Nodes") && iter->second.IsMap()) // Nodes节点解析 { for(YAML::const_iterator node_iter = iter->second.begin(); node_iter != iter->second.end(); ++node_iter) { string node_name = node_iter->first.as<string>(); if(0 == node_name.compare("Midware")) { YAML::Node module_node = node_iter->second; if(module_node["CpuModel"] && module_node["CpuModel"].IsScalar()) { string str_cpu_model = module_node["CpuModel"].as<string>(); printf("str_cpu_model:%s\n", str_cpu_model.c_str()); } } } }else if(0 == ChildName.compare("Version") && iter->second.IsScalar()) { string str_version = iter->second.as<string>(); printf("str_version:%s\n", str_version.c_str()); } } //YAML::Emitter out; return 0; }