boost使用property_tree操作xml



#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <boost/typeof/typeof.hpp>

using namespace std;
/**test.xml
<?xml version='1.0' encoding='utf-8' ?>
<config> 
    <color> 
        <red>0.1</red> 
        <green>0.1</green> 
        <blue>0.1</blue> 
        <alpha>1.0</alpha> 
    </color> 
    <size> 
        <x>640</x> 
        <y>480</y> 
    </size> 
    <mode fullscreen="false">screen mode</mode> 
</config>
 */
#include <boost/tokenizer.hpp>
void test_tokenizer()
{
    string str("Link raise the master-sword.");
   boost::tokenizer<> tok(str);     /* 使用缺省模板参数创建分词对象 */
   
   for (BOOST_AUTO(pos, tok.begin()); pos != tok.end(); ++pos)
   {
      cout << "[" << *pos << "]";
   }
   cout << endl;

}
int main(void)
{
   using namespace boost::property_tree;
   ptree pt;
   read_xml("/root/test.xml", pt);      //解析xml文件到ptree
  
   /* 示范get函数用法 */
   cout << pt.get<string>("config.mode") << endl;   
   /* 示例get_child的用法 */
   BOOST_AUTO(child, pt.get_child("config.color"));
   for (BOOST_AUTO(pos, child.begin()); pos != child.end(); ++pos)
   {
      cout << pos->second.data() << "\n";
   }
   cout << endl;
   test_tokenizer();
}

http://www.oschina.net/code/snippet_126720_4952

你可能感兴趣的:(boost使用property_tree操作xml)