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

原文链接: https://www.cnblogs.com/ganquanfu2008/p/3180798.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    C++如何读写xml文件

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/95095

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月10日 上午2:56
下一篇 2023年2月10日 上午2:57

相关推荐