C++如何读写xml文件

#pragma once

#include "persistable.h"
#include "memento.h"
#include<vector>
#include <string>
using namespace std;
class CFGStudent:public xml_api::Persistable
{
public:
CFGStudent(void);
~CFGStudent(void);

public:

virtual bool SaveState(xml_api::Memento* m);

// 读取配置
virtual bool LoadState(xml_api::Memento* m);

public:
string m_strName;
int m_nNumber;
};


typedef vector<CFGStudent> Students;

 

 

 

#pragma once
#include "FGStudent.h"
class CFGTeacher:xml_api::Persistable
{
public:
CFGTeacher(void);
~CFGTeacher(void);


public:

virtual bool SaveState(xml_api::Memento* m);

// 读取配置
virtual bool LoadState(xml_api::Memento* m);


public:
string m_strName;
int m_nAge;
Students students;
};

 

 


#include "StdAfx.h"
#include "FGConfig.h"


CFGConfig::CFGConfig(void)
{
}


CFGConfig::~CFGConfig(void)
{
}

bool CFGConfig::SaveState( xml_api::Memento* m )
{
m->SetName("demo");
xml_api::Memento* mo = m->CreateChild("teacher");

m_teacher.SaveState(mo);
return true;

}

 

 

bool CFGConfig::LoadState( xml_api::Memento* m )
{
xml_api::Memento *mo = m->GetChild(0);
string name = mo->GetName();
if ("teacher" == name)
{
m_teacher.LoadState(mo);
}
return true;
}


CFGConfig & CFGConfig::Instance()
{
static CFGConfig instance;
return instance;
}


#include "StdAfx.h"
#include "FGStudent.h"


CFGStudent::CFGStudent(void)
{
}


CFGStudent::~CFGStudent(void)
{
}

bool CFGStudent::SaveState( xml_api::Memento* m )
{
m->SetString("name", m_strName);
m->SetInteger("number", m_nNumber);

return true;
}

bool CFGStudent::LoadState( xml_api::Memento* m )
{
m_strName = m->GetString("name");
m_nNumber = m->GetInteger("number");
return true;
}

 

#include "StdAfx.h"
#include "FGTeacher.h"


CFGTeacher::CFGTeacher(void)
{
}


CFGTeacher::~CFGTeacher(void)
{
}

bool CFGTeacher::SaveState( xml_api::Memento* m )
{
m->SetString("name", m_strName);
m->SetInteger("age", m_nAge);

int count = students.size();
for (int i = 0; i < count; ++i)
{
xml_api::Memento* mo = m->CreateChild("student");
students[i].SaveState(mo);
}

return true;
}

bool CFGTeacher::LoadState( xml_api::Memento* m )
{
m_strName = m->GetString("name");
m_nAge = m->GetInteger("age");
int nchildCount;
nchildCount = m->GetChildCount();
for (int i = 0; i < nchildCount; ++i)
{
xml_api::Memento *mo = m->GetChild(i);
CFGStudent student;
student.LoadState(mo);
students.push_back(student);
}

return true;
}


#pragma once

#include "FGTeacher.h"
class CFGConfig:public xml_api::Persistable
{
protected:
CFGConfig(void);
~CFGConfig(void);


public:
static CFGConfig & Instance();

public:

 

virtual bool SaveState(xml_api::Memento* m);

// 读取配置
virtual bool LoadState(xml_api::Memento* m);


public:
CFGTeacher m_teacher;

};

 

// xmlTest.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "FGConfig.h"
#include "Utils.h"
#include "persistable.h"
#include <iostream>
using namespace std;
using namespace xml_api;

#include <crtdbg.h>
inline void EnableMemLeakCheck()
{
_CrtSetDbgFlag(_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG) | _CRTDBG_LEAK_CHECK_DF);
}
#ifdef _DEBUG
#define new new(_NORMAL_BLOCK, __FILE__, __LINE__)
#endif

 

int _tmain(int argc, _TCHAR* argv[])
{
Utils utils;
CFGConfig::Instance().m_teacher.m_strName = "梁毅";
CFGConfig::Instance().m_teacher.m_nAge = 28;

CFGStudent student;
student.m_nNumber = 1002;
student.m_strName = "李刚";

CFGConfig::Instance().m_teacher.students.push_back(student);

 

student.m_nNumber = 1003;
student.m_strName = "方如波";

CFGConfig::Instance().m_teacher.students.push_back(student);
utils.Serialize("my.xml", &CFGConfig::Instance());
/*
utils.DeserializeFromFile("my.xml", &CFGConfig::Instance());
CFGConfig::Instance().m_teacher.m_strName = "李白";
utils.Serialize("update.xml", &CFGConfig::Instance());*/


EnableMemLeakCheck();
char* p = new char[100];

int wait;
cin >> wait;

return 0;
}

posted @ 2013-07-09 19:48  Predator  阅读(2275)  评论(0编辑  收藏  举报