Header file:
#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::p roperty_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
Source file:
#include "BoostXmlUtil.h" Boostxmlutil::boostxmlutil () {//ctor} boostxmlutil::~boostxmlutil () {//dtor} MAP&L
t;string,string>* boostxmlutil::d sn (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;
}
Test:
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);