当前位置:编程学习 > XML/UML >>

boost::xml_parser 读取xml

boost::xml_parser 处理xml
---------debug_settings.xml--------
[html] 
<root> 
  <strage name="asd" alias="13" classId="13.22">r 
    <id>12</id>   
    <name>"jack"</name>   
    <accout>"0001"</accout> 
  </strage> 
 <strage name="abbasd" alias="14" classId="14.22">abb 
    <id >13</id>   
    <name>"toni"</name>   
    <accout>"0002"</accout> 
  </strage> 
 <strage name="assasd" alias="15" classId="15.44">ass 
    <id >14</id>   
    <name>"hate"</name>   
    <accout>"0003"</accout> 
  </strage> 
</root> 

--------main.cpp--------
[html] 
#include <boost/property_tree/ptree.hpp> 
 
#include <boost/property_tree/xml_parser.hpp> 
 
#include <boost/algorithm/string.hpp> 
 
#include <boost/foreach.hpp> 
 
#include <string> 
 
#include <set> 
 
#include <vector> 
 
#include <exception> 
 
#include <iostream> 
 
  
 
using namespace std; 
 
  
 
static const string str_node_name[] = {"id","name","accout"}; 
 
  
 
struct xmlStageDetail 
 

 
    string id; 
 
    string name; 
 
    string accout; 
 
}; 
 
  
 
int load(const char* filename,vector<xmlStageDetail>*m_xml_detail) 
 

 
    xmlStageDetail xml_stage_detail; 
 
    using boost::property_tree::ptree; 
 
    ptree pt; 
 
    ptree root; 
 
    try 
 
    { 
 
        read_xml(filename, pt); 
 
        cout << "获取数据" << pt.data()<< endl; 
 
        root = pt.get_child("root"); 
 
    } 
 
    catch (std::exception& e) 
 
    { 
 
        std::cout << "Error: " << e.what() << endl; 
 
        return -1; 
 
    } 
 
  
 
    cout << "root的长度:" << root.size() << endl; 
 
    int ita = pt.get<int>("root.strage.id"); 
 
    string cattr = pt.get<string>("root.strage.<xmlattr>.name"); 
 
    cout << "root.strage.id:" << ita << "\troot.strage.<xmlattr>.name:" << cattr << endl; 
 
  
 
    for (ptree::iterator itr = root.begin();itr!=root.end();itr++) 
 
    { 
 
        ptree strage = itr->second; 
 
        
 
        string ssv = strage.data(); 
 
        string sattr = strage.get<string>("<xmlattr>.name"); 
 
        int iattr = strage.get<int>("<xmlattr>.alias"); 
 
        float fattr = strage.get<float>("<xmlattr>.classId"); 
 
        boost::trim<string>(ssv); // 去除首尾空格 
 
        cout << ssv << "[\tname:" << sattr << "\talias:" << iattr<< "\tclassid:" << fattr << endl; 
 
        for (int i = 0;i<3;i++) 
 
        { 
 
            ptree pt; 
 
            string str_value; 
 
            try 
 
            { 
 
                pt = strage.get_child(str_node_name[i]); 
 
                str_value = pt.data(); 
 
                cout << str_value << endl; 
 
            } 
 
            catch (std::exception& e) 
 
            { 
 
                cout<< str_value <<"解析错误:" << e.what() << endl; 
 
                return -1; 
 
            } 
 
            switch (i) 
 
            { 
 
            case 0: 
 
                xml_stage_detail.id = str_value; 
 
                break; 
 
            case 1: 
 
                xml_stage_detail.name = str_value; 

补充:web前端 , HTML 5 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,