头文件:
#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);
}