欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

boost::xml_parser 读取xml

程序员文章站 2022-10-31 09:08:47
boost::xml_parser 处理xml ---------debug_settings.xml-------- [html]  &n...

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; 
 
  
 
                break; 
 
            case 2: 
 
                xml_stage_detail.accout = str_value; 
 
  
 
                break; 
 
            } 
 
  
 
        } 
 
        m_xml_detail->push_back(xml_stage_detail); 
 
    } 
 
    return 0; 
 
  
 

 
  
 
  
 
int main() 
 

 
    vector<xmlStageDetail>m_xml_detail; 
 
    try 
 
    { 
 
        load("debug_settings.xml",&m_xml_detail); 
 
        std::cout << "Success\n"; 
 
    } 
 
    catch (std::exception &e) 
 
    { 
 
        std::cout << "Error: " << e.what() << endl; 
 
        return -1; 
 
    } 
 
    return 0; 
 

运行结果如下:

 boost::xml_parser 读取xml