boost库读写XML Json文件[转]
目录
boost库读写XML文件
参考:《Boost学习之读写xml文件》
- 引入头文件:
#include <boost/property_tree/ptree.hpp> #include <boost/property_tree/xml_parser.hpp>
构建xml示例
void build_xml() { boost::property_tree::ptree root; // 构建根节点 boost::property_tree::ptree resource, version; version.put<int>("Version", 1); // 创建子节点 root.add_child("root", version); // 将子节点添加到根节点下 boost::property_tree::ptree student; student.put<std::string>("Name", "zhangsan"); student.put<int>("Age", 23); student.put<std::string>("Sex", "man"); student.put<int>("Grade", 2); // 可以用“根节点.子节点”的方式添加子节点 root.add_child("root.Student", student); boost::property_tree::ptree course, tnull; course.put<std::string>("Chinese", "runtu"); course.put<std::string>("Math", "sin"); course.add_child("English", tnull); root.add_child("root.Course", course); // 使用setting参数,保证文件内容样式有缩进有对齐. auto setting = boost::property_tree::xml_writer_make_settings<std::string>('\t', 1); //写入文件,把proptrt_tree转换为xml格式 boost::property_tree::write_xml("config_xml.xml", root, std::locale(), setting); }
- 输出:
读取xml示例
void readxml() { boost::property_tree::ptree root; //构建根节点 try { boost::property_tree::read_xml("config_xml.xml", root); } catch (const std::exception& e) { std::cout << e.what() << std::endl; } int version = root.get<int>("root.Version"); // 获取指定节点的值 std::cout << "Version is : " << version << std::endl; boost::property_tree::ptree student; std::cout << "student" << std::endl; student = root.get_child("root.Student"); // 获取指定节点,迭代遍历输出 for (boost::property_tree::ptree::iterator it = student.begin(); it != student.end(); ++it) { std::cout << "\t" << it->first; std::cout << ":" << it->second.data() << std::endl; } }
- 输出:
修改xml示例
void updatexml() { boost::property_tree::ptree root; try { //读取xml,必须要用boost::property_tree::xml_parser::trim_whitespace换行以及裁剪空格, //否则写入的文件格式会错乱 boost::property_tree::read_xml("config_xml.xml", root, \ boost::property_tree::xml_parser::trim_whitespace); } catch (const std::exception& e) { std::cout << e.what() << std::endl; } //修改Version版本为 root.put("root.Version", 2); // 没有Prov节点,将添加新节点Prov root.put("root.Student.Prov", "beijing"); // 设置写入xml的格式,否则格式会错乱,不易于阅读 boost::property_tree::xml_parser::xml_writer_settings<std::string> settings = \ boost::property_tree::xml_writer_make_settings<std::string>('\t', 1); //把property_tree 转为XML文件 write_xml("config_xml.xml", root, std::locale(), settings); }
- 输出:
获取属性
<?xml version="1.0" encoding="utf-8"?> <root> <Item name="demo" datalen="size" datatype="int"> <SubItem name="demo1" datalen="size1" datatype="int">11</SubItem> <SubItem name="demo2" datalen="size2" datatype="int">111</SubItem> <SubItem name="demo3" datalen="size3" datatype="int">1111</SubItem> <SubItem name="demo4" datalen="size4" datatype="int">11111</SubItem> </Item> </root>
void testGetAttr() { boost::property_tree::ptree root; std::string strAttrName; std::map<std::string, std::string> mapAtrr; std::map< std::string, std::map<std::string, std::string>> mapTal; try { boost::property_tree::read_xml("configXml.xml", root); } catch (const std::exception& e) { std::cout << e.what() << std::endl; } BOOST_FOREACH(boost::property_tree::ptree::value_type & v1, root.get_child("root")) { if (v1.first == "Item") { strAttrName = v1.second.get<std::string>("<xmlattr>.datalen"); } BOOST_FOREACH(boost::property_tree::ptree::value_type & v2, v1.second) { if (v2.first == "SubItem") { std::string strSubAttrName = v2.second.get<std::string>("<xmlattr>.datalen");//取属性 std::string value = v2.second.data(); mapAtrr.insert(std::pair<std::string, std::string>(strSubAttrName, value)); } } mapTal.insert(std::pair< std::string, std::map<std::string, std::string>>(strAttrName, mapAtrr)); } std::cout << "mapTal.size() = " << mapTal.size() << std::endl; for (auto v = mapTal.begin(); v != mapTal.end(); ++v) { for (auto u = (v->second).begin(); u != (v->second).end(); ++u) { std::cout << " " << v->first << " " << u->first << " " << u->second << std::endl; } } }
- 输出:
boost库读写Json文件
参考:《Boost学习之读写json》
- json格式,在boost中也被抽象为 property_tree。
Boost中Json数据类型
- 在构建json文件时,我们只需要构建多个tree节点,按照对应的树形结构组合在一起即可。
- 从boost写json文件API源码可以看出,property_tree支持三种类型,分别是value 、array, object。
- value:系统默认数据类型;
- array:数组类型,可以嵌套自定义类型,在方括号中存放。
- object:使用者自定义的数据类型,在花括号中存放。
- 引入头文件:
#include <boost/property_tree/ptree.hpp> #include <boost/property_tree/json_parser.hpp>
简单的Json示例
void build_json() { boost::property_tree::ptree root; // 根节点 boost::property_tree::ptree arr; root.put("version", 1); // value类型,一级节点 // 二级节点 boost::property_tree::ptree node1, node2, node3, node4, node5; node1.put("frame_rate", 0); node1.put("bit_rate", 1); node1.put("resolution", 3); node1.put("boot", 0); node1.put("ssfs", 2); // 添加二级节点 root.add_child("0", node1); root.add_child("1", node1); root.add_child("2", node1); std::stringstream s; boost::property_tree::json_parser::write_json(s, root, false); std::string out = s.str(); std::ofstream oftream("config_json.json", std::ios_base::out); oftream << out; oftream.close(); }
- 运行结果
{ "version": "1", "0": { "frame_rate": "0", "bit_rate": "1", "resolution": "3", "boot": "0", "ssfs": "2" }, "1": { "frame_rate": "0", "bit_rate": "1", "resolution": "3", "boot": "0", "ssfs": "2" }, "2": { "frame_rate": "0", "bit_rate": "1", "resolution": "3", "boot": "0", "ssfs": "2" } }
简单Json解析示例
- 一般有写入就有读取,下面示例代码,将结合上述json结构,读取相关信息,并打印
void Read_json() { std::ifstream ifstrema("config_json.json", std::ios_base::in); boost::property_tree::ptree node; //初始化根节点 boost::property_tree::read_json(ifstrema, node); // 将json文件读入根节点 int intVersion = node.get<int>("version"); // 获取一级节点“version”的值 boost::property_tree::ptree temp = node.get_child("0"); // 获取一级节点“0”的子节点信息 // 迭代一级节点的子节点中的每个项 for (boost::property_tree::ptree::iterator it = temp.begin(); it != temp.end(); ++it) { std::cout << "first:" << it->first << std::endl; // key std::cout << "second:" << it->second.data() << std::endl; // value } }
- 可以看出,json是按照键值对存储,读取时,通过键获取相对应的值。
较复杂的Json示例
void build_json2() { boost::property_tree::ptree root, node1, object; boost::property_tree::ptree arr; boost::property_tree::ptree arrv1, arrv2, arrv3; // value 类型 root.put("version", 1); bool boolvalue = true; root.put("boolean", boolvalue); root.put("null", "null"); root.put("tip", "Hello World"); node1.put("frame_rate", 0); node1.put("bit_rate", 1); node1.put("resolution", 3); node1.put("boot", 0); node1.put("ssfs", 2); root.add_child("0", node1); root.add_child("10", node1); root.add_child("20", node1); // arry类型 arrv1.put("a", "b"); arrv2.put("Tom", "23"); arrv3.put("Tony", "4"); arrv1.put("marble", "2222"); arr.push_back(std::make_pair("", arrv1)); arr.push_back(std::make_pair("", arrv2)); arr.push_back(std::make_pair("", arrv3)); // object类型 object.put("a", "b"); object.put("c", "d"); object.put("e", "f"); root.add_child("1", arr); root.add_child("2", arr); root.add_child("object", object); std::stringstream s; boost::property_tree::write_json(s, root, false); std::string out = s.str(); std::ofstream oftream("config_json.json", std::ios_base::out); oftream << out; oftream.close(); std::cout << out; }
- 执行结果
{ "version": "1", "boolean": "true", "null": "null", "tip": "Hello World", "0": { "frame_rate": "0", "bit_rate": "1", "resolution": "3", "boot": "0", "ssfs": "2" }, "10": { "frame_rate": "0", "bit_rate": "1", "resolution": "3", "boot": "0", "ssfs": "2" }, "20": { "frame_rate": "0", "bit_rate": "1", "resolution": "3", "boot": "0", "ssfs": "2" }, "1": [{ "a": "b", "marble": "2222" }, { "Tom": "23" }, { "Tony": "4" }], "2": [{ "a": "b", "marble": "2222" }, { "Tom": "23" }, { "Tony": "4" }], "object": { "a": "b", "c": "d", "e": "f" } }
较复杂的Json读取
void Read_json2() { std::fstream ifstrema("config_json.json", std::ios_base::in); boost::property_tree::ptree node; boost::property_tree::read_json(ifstrema, node); int intVersion = node.get<int>("version"); // 迭代获取对象 boost::property_tree::ptree temp = node.get_child("0"); for (boost::property_tree::ptree::iterator it = temp.begin(); it != temp.end(); ++it) { std::cout << "first:" << it->first << std::endl; std::cout << "second:" << it->second.data() << std::endl; } boost::property_tree::ptree objNode = node.get_child("object"); for (boost::property_tree::ptree::iterator it = objNode.begin(); it != objNode.end(); ++it) { std::cout << "object fist: " << it->first << std::endl; std::cout << "object second: " << it->second.data() << std::endl; } // 获取数组对象,先获取迭代器首地址,然后按照键值对获取值 boost::property_tree::ptree arr = node.get_child("1"); boost::property_tree::ptree::iterator it = arr.begin(); std::cout << it->second.get<std::string>("a") << std::endl; std::cout << it->second.get<std::string>("marble") << std::endl; it++; // 迭代器++,指针指向下一个数组首地址。 std::cout << it->second.get<std::string>("Tom") << std::endl; it++; }