<boost> xml文件解析 例3

conf.xml文件:

<?xml version="1.0" encoding="utf-8"?>

<span style="font-size:14px;">
  <root> 
    <students> 
      <name>张三</name>  
      <name>李四</name>  
      <name>王二</name> 
    </students> 
  </root>
</span>

cpp文件:

#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <iostream>
 
using namespace std;
using namespace boost::property_tree;
 
int main()
{
    ptree pt; 
    //open xml and read information to pt
    read_xml("conf.xml", pt);
    //iter all child value
    auto child = pt.get_child("span.root.students");
    for (auto i = child.begin();i!=child.end();++i)
    {   
        string name = i->second.get_value<string>();//此时i->first的值为路径名:name
        cout<<name<<endl;
    }   
 
    return 0;

编译:

g++ 00_xml.cpp -I/root/boost_1_75_0_build/include -std=c++11

执行结果:

张三
李四
王二
posted @ 2021-03-22 16:43  kouei_kou  阅读(41)  评论(0)    收藏  举报