使用boost读取XML文件详细介绍

boost读取XML文件

boost中提供了对配置文件读取的支持,它就是:property_tree。

    basic_ptree 是property_tree的核心基础。其接口像std::list。可以执行很多基本的元素操作,比如使用begin()、end()等。
    此外还加入了操作属性树的get()、get_child()、get_value()、data()等额外的操作。

    basic_ptree有两个重要的内部定义self_type和value_type。self_type是basic_ptree模板实例化后自身的类型,它也是子节点的类型。value_type是节点的数据结构,它是一个std::pair,它含有属性名(first)和节点自身(second)。

    通常不使用basic_ptree,而是使用预定义的typedef。ptree、wptree、iptree、wiptree。前缀i表示忽略大小写,前缀w表示支持宽字符。

例如:

config.xml

 
 
 1 
 fansy 
  
  http://blog.csdn.net//fansongy 
  http://weibo.com//fansongy 
  
  

我要读取它的数据:



#include  
#include  
#include  
#include   
using namespace std; 
using namespace boost::property_tree; 
int main() 
{ 
  ptree pt; 
  read_xml("conf.xml",pt);   //读入一个xml文件 
  cout<<"ID is "<("con.id")<("con.no_prop",100)<("name")<second.data()< 
 


运行的显示为: 
ID is 1 
Try Default100 
name is :fansy 
    http://blog.csdn.net//fansongy 
    http://weibo.com//fansongy 



config.xml为: 
 
 
 1 
 Sword 
 
   http://blog.csdn.net//fansongy 
   http://weibo.com//fansongy 
   http://www.baidu.com 
 
  



感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

你可能感兴趣的:(使用boost读取XML文件详细介绍)