基于boost简单读写xml文件笔记

头文件:

#ifndef BOOSTXMLUTIL_H
#define BOOSTXMLUTIL_H
#include <iostream>
#include <string>
#include <map>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <boost/foreach.hpp>
#include <boost/algorithm/string.hpp>
using namespace std;
using namespace boost::property_tree;

class BoostXmlUtil {
public:
	BoostXmlUtil();
	virtual ~BoostXmlUtil();
	map<string, string>* readXml(string& filename);
	int writexml(map<string, string>& mapxml, string& path);
	map<string, string>* dsn(string&);
	map<string, string>* file(string&);

protected:
private:
	map<string, string> *config=NULL;
};

#endif // BOOSTXMLUTIL_H


源文件:

#include "BoostXmlUtil.h"

BoostXmlUtil::BoostXmlUtil() {
    //ctor
}

BoostXmlUtil::~BoostXmlUtil() {
    //dtor
}
map<string,string>* BoostXmlUtil::dsn(string& filename) {
    // string s = " hello boost!! ";
    // boost::algorithm::trim(s);
    // cout << s << endl;
    //char filename[] = "testConfigRead.xml";
    //cout<<filename;
    ptree* tree=new ptree;
    ptree* root=new ptree;
    xml_parser::read_xml(filename, *tree);
    *root = tree->get_child("dsn");


    map<string,string>* config=new map<string,string>;
    for (ptree::iterator it = root->begin(); it!=root->end(); it++) {
        ptree node = it->second;
        string name=it->first;
        string value=node.data();
        //cout<<name;
        //cout<<value;
        boost::algorithm::trim(name);
        boost::algorithm::trim(value);
        (*config).insert(pair <string, string>(name,value));
    }
    delete tree;
    delete root;
    return config;
}
map<string,string>* BoostXmlUtil::file(string& filename) {
    // string s = " hello boost!! ";
    // boost::algorithm::trim(s);
    // cout << s << endl;
    //char filename[] = "testConfigRead.xml";
    //cout<<filename;
    ptree* tree=new ptree;
    ptree* root=new ptree;
    xml_parser::read_xml(filename, *tree);
    *root = tree->get_child("file");


    map<string,string>* config=new map<string,string>;
    for (ptree::iterator it = root->begin(); it!=root->end(); it++) {
        ptree node = it->second;
        string name=it->first;
        string value=node.data();
        //cout<<name;
        //cout<<value;
        boost::algorithm::trim(name);
        boost::algorithm::trim(value);
        (*config).insert(pair <string, string>(name,value));
    }
    delete tree;
    delete root;
    return config;
}

map<string,string>* BoostXmlUtil::readXml(string& filename) {
    // string s = " hello boost!! ";
    // boost::algorithm::trim(s);
    // cout << s << endl;
    //char filename[] = "testConfigRead.xml";
    //cout<<filename;
    ptree* tree=new ptree;
    ptree* root=new ptree;
    xml_parser::read_xml(filename, *tree);
    *root = tree->get_child("root");


    map<string,string>* config=new map<string,string>;
    for (ptree::iterator it = root->begin(); it!=root->end(); it++) {
        ptree node = it->second;
        string name=it->first;
        string value=node.data();
        //cout<<name;
        //cout<<value;
        boost::algorithm::trim(name);
        boost::algorithm::trim(value);
        (*config).insert(pair <string, string>(name,value));
    }
    delete tree;
    delete root;
    return config;
}
int BoostXmlUtil::writexml(map<string,string>& mapxml,string& path) {
    ptree pt;
    map<string,string>::iterator it;
    for(it=mapxml.begin(); it!=mapxml.end(); ++it) {
        //cout<<"key: "<<it->first <<" value: "<<it->second<<endl;
        pt.put<string>("root."+it->first, it->second);
    }
    write_xml(path.c_str() , pt);
    return 0;
}

测试:

void testxml() {
    BoostXmlUtil* xml = new BoostXmlUtil;
    string filename("./dsn.xml");
    map<string, string>* newMap = xml->dsn(filename);
//	newMap->insert(pair<string, string>("a", "d"));
    std::map<string, string>::iterator iter = newMap->find("dsn");

    string &s = iter->second;
    cout << s << endl;

    filename="./file.xml";
    newMap = xml->file(filename);
//	newMap->insert(pair<string, string>("a", "d"));
    iter = newMap->find("location");

    s = iter->second;
    cout << s << endl;

    s = "jjjj";

    string path = "testConfigRead.xml";
    xml->writexml(*newMap, path);
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值