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