boost 实现xml 解析
资源内容介绍
boost中提供了对配置文件读取的支持:property_tree。property_tree的核心基础是basic_ptree。其接口像std::list。可以执行很多基本的元素操作,比如使用begin()、end()等。此外还加入了操作属性树的get()、get_child()、get_value()、data()等额外的操作。 #include <stdio.h>#include <iostream>#include <string>#include <boost/property_tree/ptree.hpp>#include <boost/property_tree/xml_parser.hpp>#include <boost/foreach.hpp>using namespace std;using namespace boost::property_tree;using namespace boost::property_tree::xml_parser;int main(){ string attrStr = ""; /*cout <<"********SigRaw.xml*************"<<endl; ptree pt1; read_xml("SigRaw.xml",pt1); attrStr = pt1.get<string>("root.type"); cout << "attrStr:type---" << attrStr << endl; attrStr = pt1.get<string>("root.separator"); cout << "attrStr:separator---" << attrStr << endl; BOOST_FOREACH(ptree::value_type& v, pt1.get_child("root.fields")) { attrStr = v.second.get<string>("<xmlattr>.name"); cout << "attrStr:name---" << attrStr << endl; attrStr = v.second.get<string>("<xmlattr>.id"); cout << "attrStr:id---" << attrStr << endl; attrStr = v.second.get<string>("<xmlattr>.type"); cout << "attrStr:type---" << attrStr << endl; attrStr = v.second.get<string>("<xmlattr>.len"); cout << "attrStr:len---" << attrStr << endl; attrStr = v.second.get<string>("<xmlattr>.defvalue"); cout << "attrStr:defvalue---" << attrStr << endl; } */ cout <<"********SigRaw_CQ.xml*************"<<endl; ptree pt2; read_xml("SigRaw_CQ.xml",pt2); BOOST_FOREACH(ptree::value_type& v, pt2.get_child("config.struct")) { //cout << "v.first: " << v.first << endl; if(v.first == "<xmlattr>"){ attrStr = v.second.get<string>("name"); cout << "attrStr:struct name---" << attrStr << endl; attrStr = v.second.get<string>("alias"); cout << "attrStr:struct alias---" << attrStr << endl; } else if (v.first == "rowkey") {cout << "v.first: " << v.first << endl;attrStr = v.second.get<string>("<xmlattr>.class");cout << "attrStr:rowkey class---" << attrStr << endl; ptree pt3 = pt2.get_child("config.struct.rowkey");BOOST_FOREACH(ptree::value_type &v1, pt3){ cout << "v1.first--: " << v1.first << endl;//sectionattrStr = pt3.get<string>("config.struct.rowkey.field.section");cout << "attrStr:field section---" << attrStr << endl;attrStr = pt3.get<string>("config.struct.rowkey.field.name");cout << "attrStr:field name---" << attrStr << endl;attrStr = pt3.get<string>("config.struct.rowkey.field.id");cout << "attrStr:field id---" << attrStr << endl;attrStr = pt3.get<string>("config.struct.rowkey.field.length");cout << "attrStr:field length---" << attrStr << endl;attrStr = pt3.get<string>("config.struct.rowkey.field.defvalue");cout << "attrStr:field defvalue---" << attrStr << endl;//valueattrStr = v1.second.get<string>("config.struct.rowkey.field.value");cout << "attrStr:field value---" << attrStr << endl;attrStr = v1.second.get<string>("config.struct.rowkey.field.name");cout << "attrStr:field name---" << attrStr << endl;attrStr = v1.second.get<string>("config.struct.rowkey.field.id");cout << "attrStr:field id---" << attrStr << endl;attrStr = v1.second.get<string>("config.struct.rowkey.field.length");cout << "attrStr:field length---" << attrStr << endl;//normalattrStr = v1.second.get<string>("config.struct.rowkey.field.name");cout << "attrStr:field name---" << attrStr << endl;attrStr = v1.second.get<string>("config.struct.rowkey.field.id");cout << "attrStr:field id---" << attrStr << endl;attrStr = v1.second.get<string>("config.struct.rowkey.field.type");cout << "attrStr:field type---" << attrStr << endl;attrStr = v1.second.get<string>("config.struct.rowkey.field.length");cout << "attrStr:field length---" << attrStr << endl;attrStr = v1.second.get<string>("config.struct.rowkey.field.defvalue");cout << "attrStr:field defvalue---" << attrStr << endl;attrStr = v1.second.get<string>("config.struct.rowkey.field.decode");cout << "attrStr:field decode---" << attrStr << endl;attrStr = v1.second.get<string>("config.struct.rowkey.field.placeholder");cout << "attrStr:field placeholder---" << attrStr << endl;attrStr = v1.second.get<string>("config.struct.rowkey.field.pos_placeholder");cout << "attrStr:field pos_placeholder---" << attrStr << endl;} } else if(v.first == "timestamp") { attrStr = v.second.get<string>("<xmlattr>.class"); cout << "attrStr:timestamp class---" << attrStr << endl; attrStr = v.second.get<string>("<xmlattr>.formula"); cout << "attrStr:timestamp formula---" << attrStr << endl; } else if(v.first == "indexs") { BOOST_FOREACH(ptree::value_type &v1, v.second) { attrStr = v1.second.get<string>("<xmlattr>.name"); cout << "attrStr:indexs cf name---" << attrStr << endl; attrStr = v1.second.get<string>("<xmlattr>.class"); cout << "attrStr:indexs cf class---" << attrStr << endl; attrStr = v1.second.get<string>("field.<xmlattr>.name"); cout << "attrStr:field name---" << attrStr << endl; attrStr = v1.second.get<string>("field.<xmlattr>.id"); cout << "attrStr:field id---" << attrStr << endl; attrStr = v1.second.get<string>("field.<xmlattr>.type"); cout << "attrStr:field type---" << attrStr << endl; attrStr = v1.second.get<string>("field.<xmlattr>.length"); cout << "attrStr:field length---" << attrStr << endl; attrStr = v1.second.get<string>("field.<xmlattr>.defvalue"); cout << "attrStr:field defvalue---" << attrStr << endl; attrStr = v1.second.get<string>("field.<xmlattr>.decode"); cout << "attrStr:field decode---" << attrStr << endl; attrStr = v1.second.get<string>("field.<xmlattr>.placeholder"); cout << "attrStr:field placeholder---" << attrStr << endl; attrStr = v1.second.get<string>("field.<xmlattr>.pos_placeholder"); cout << "attrStr:field pos_placeholder---" << attrStr << endl; }//end BOOST_FOREACH } else if(v.first == "ag") { BOOST_FOREACH(ptree::value_type &v1, v.second) { //cout << "v1.first: " << v1.first << endl; if(v1.first == "<xmlattr>"){ attrStr = v1.second.get<string>("name"); cout << "attrStr:ag name---" << attrStr << endl; } else { BOOST_FOREACH(ptree::value_type &v2, v1.second) { //cout << "v2.first: " << v2.first << endl; if(v2.first == "<xmlattr>"){ attrStr = v2.second.get<string>("name"); cout << "attrStr:cf name---" << attrStr << endl; } else { BOOST_FOREACH(ptree::value_type &v3, v2.second) { //cout << "v3.first: " << v3.first << endl; if(v3.first == "<xmlattr>"){ //it's an attribute attrStr = v3.second.get<string>("name"); cout << "attrStr:cq name---" << attrStr << endl; attrStr = v3.second.get<string>("type"); cout << "attrStr:cq type---" << attrStr << endl; attrStr = v3.second.get<string>("seperator"); cout << "attrStr:cq seperator---" << attrStr << endl; } else { attrStr = v3.second.get<string>("<xmlattr>.name"); cout << "attrStr:field name---" << attrStr << endl; attrStr = v3.second.get<string>("<xmlattr>.id"); cout << "attrStr:field id---" << attrStr << endl; attrStr = v3.second.get<string>("<xmlattr>.type"); cout << "attrStr:field type---" << attrStr << endl; attrStr = v3.second.get<string>("<xmlattr>.length"); cout << "attrStr:field length---" << attrStr << endl; attrStr = v3.second.get<string>("<xmlattr>.defvalue"); cout << "attrStr:field defvalue---" << attrStr << endl; } }//end BOOST_FOREACH } }//end