Simple Read and write XML file notes based on boost

Source: Internet
Author: User
Tags dsn trim

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);



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.