##################################################
#include < boost/property_tree/ptree.hpp >
#include < boost/property_tree/xml_parser.hpp >
using namespace boost::property_tree;
//read_xml() input xml file
void read_xml(const string &, ptree &pt);
void read_xml(basic_istream &, ptree &pt);
//write_xml() output xml file
void write_xml(const string &, ptree &pt);
void write_xml(basic_ostream &, ptree &pt);
//mark rapidxml (learn it)
##################################################
#include < iostream >
#include < string >
#include < assert.h >
#include < boost/assign.hpp >
#include < boost/typeof/typeof.hpp >
#include < boost/property_tree/ptree.hpp >
#include < boost/property_tree/xml_parser.hpp >
#include < boost/property_tree/json_parser.hpp >
using namespace std;
using namespace boost;
using namespace boost::property_tree;
int main()
{
{
ptree pt;
read_xml("conf.xml", pt);
cout << pt.get< string >("conf.theme") << endl;
cout << pt.get< int >("conf.gui") << endl;
BOOST_AUTO(child, pt.get_child("conf.urls"));
for(BOOST_AUTO(it, child.begin()); it != child.end(); ++it)
{
cout < < it->first << " " << it->second.get_value() << endl;
}
}
cout << endl;
{
ptree pt;
read_xml("conf.xml", pt);
pt.put("conf.theme", "Matrix Reloaded");
pt.put("conf.clock_style", 12);
pt.put("conf.gui", 10);
pt.add("conf.urls.url", "http://www.url4.org");
pt.add("conf.block", 10);
write_xml("out.xml", pt);
}
cout << endl;
{
ptree pt;
read_xml("conf.xml", pt);
ptree::assoc_iterator it = pt.find("a");
cout << it->first << " " << it->second.data() << endl;
cout < < pt.get_optional("c") << endl;
assert(!pt.get_optional< string >("not_exist"));
}
cout << endl;
{
ptree pt;
read_xml("conf.xml", pt);
cout < < pt.get("conf.") << endl;
cout < < pt.get("conf.clock_style..name") << endl;
cout < < pt.get("conf.theme..id") << endl;
cout < < pt.get("conf.urls.") << endl;
}
cout << endl;
{
ptree pt;
read_json("conf.json", pt);
cout < < pt.get("conf.theme") << endl;
cout < < pt.get("conf.clock_style") << endl;
cout < < pt.get("conf.gui") << endl;
cout << pt.get("conf.no_prop", 100) << endl;
BOOST_AUTO(child, pt.get_child("conf.urls"));
for(BOOST_AUTO(it, child.begin()); it != child.end(); ++it)
{
cout << it->first << " " << it->second.data() << endl;
}
}
cout << endl;
{
ptree pt;
read_xml("conf.xml", pt, xml_parser::no_concat_text |
xml_parser::no_comments | xml_parser::trim_whitespace);
}
return 0;
#include < boost/property_tree/ptree.hpp >
#include < boost/property_tree/xml_parser.hpp >
using namespace boost::property_tree;
//read_xml() input xml file
void read_xml(const string &, ptree &pt);
void read_xml(basic_istream &, ptree &pt);
//write_xml() output xml file
void write_xml(const string &, ptree &pt);
void write_xml(basic_ostream &, ptree &pt);
//mark rapidxml (learn it)
##################################################
#include < iostream >
#include < string >
#include < assert.h >
#include < boost/assign.hpp >
#include < boost/typeof/typeof.hpp >
#include < boost/property_tree/ptree.hpp >
#include < boost/property_tree/xml_parser.hpp >
#include < boost/property_tree/json_parser.hpp >
using namespace std;
using namespace boost;
using namespace boost::property_tree;
int main()
{
}
// 其他
class GlobalConfig : public boost::property_tree::ptree
{
public:
GlobalConfig(const std::string& configFile) {
try {
boost::property_tree::xml_parser::read_xml(configFile,
*(static_cast<boost::property_tree::ptree*>(this)));
} catch (...) {
std::cout << "xml: read config File: " <<
configFile << " failed!" << std::endl;
}
}
};
extern GlobalConfig g_config;