struct ptree { data_type data; // list<pair<key_type, ptree>> children; // };
boost::property_tree::ptree heroTree; heroTree.put("Name", "John"); heroTree.put("Exp", 150); heroTree.put("Inventory.Weapon", "Blue Sword"); heroTree.put("Inventory.Money", 3000);
heroTree.put("Inventory.Item", "Stone"); heroTree.put("Inventory.Item", "Golden helmet"); heroTree.put("Inventory.Item", "Thomb key");
boost::property_tree::ptree inventoryTree = heroTree.get_child("Inventory"); inventoryTree.put("Item", "Shield of Honor");
int exp = pt.get<int>("Exp"); std::string weapon = pt.get<std::string>("Inventory.Weapon");
int exp = pt.get<int>("Exp", 0);
BOOST_FOREACH(auto &v, inventoryTree) { if (v.first == "Item") { std::cout << "Hero has item: " << v.second.get<std::string>("") << std::endl; } }
//XML- std::string xmlCode = "<ButtonList>\ <Button>B1</Button>\ <Button>B2</Button>\ </ButtonList>"; // std::stringstream stream(xmlCode); try { boost::property_tree::ptree propertyTree; // XML boost::property_tree::read_xml(stream, propertyTree); // : BOOST_FOREACH(auto &v, propertyTree) { std::cout << "Button is " << v.second.get<std::string>("") << std::endl; } // propertyTree.put("ButtonList.Button", "B3"); propertyTree.put("ButtonList.Button", "B4"); std::stringstream output_stream; // boost::property_tree::write_xml(output_stream, propertyTree); // XML std::string outputXmlCode = output_stream; } catch(boost::property_tree::xml_parser_error) { std::cout<<"XML parser error!"<<std::endl; throw; }
//XML- std::string xmlCode = "<Data name="Position" x="5" y="5"/>"; //... XML // std::string name = propertyTree.get<std::string>("Data.<xmlattr>.name"); int x = propertyTree.get<std::string>("Data.<xmlattr>.x"); int y = propertyTree.get<std::string>("Data.<xmlattr>.y");
BOOST_FOREACH(auto &v, propertyTree) { if (v.first == "Button") // { std::cout << "Button is " << v.second.get<std::string>("") << std::endl; } }
Source: https://habr.com/ru/post/168951/
All Articles