一、序列化与反序列化
??
??
??
二、反射机制
??反射机制是一种在运行时动态地获取类型信息、访问对象属性和调用对象方法的机制。在程序运行时,可以通过反射机制获取到类的名称、方法、属性等信息,并且可以动态地创建对象、调用方法和修改属性。这种机制通常用于开发框架、插件系统和代码生成器等应用程序中。
??在Java和C#等面向对象编程语言中,反射机制被广泛使用。例如,在Java中,可以使用反射机制获取一个类的Class对象,并通过该对象获取类的构造函数、属性和方法等信息。在C#中,可以使用反射机制获取一个类型的Type对象,并通过该对象获取类型的成员和属性等信息。
??在C++中,由于缺乏内置的反射机制,xml对象
三、序列化实现
??本文基于C++ tinyxml2库实现了XmlSerializer对象的封装。通过调用Serialize和Deserialize函数,可以方便地进行xml对象的
??以Student对象为例,通过调用相应的接口,可以实现对Student对象的
#include "XmlSerializer.h" class Grade { public: std::string courseName; std::string teacherName; int score; void toXml(XMLElement* elem) const { XmlSerializer::writeElement(elem, "CourseName", courseName); XmlSerializer::writeElement(elem, "TeacherName", teacherName); XmlSerializer::writeElement(elem, "Score", score); } void fromXml(XMLElement* elem) { XmlSerializer::readElement(elem, "CourseName", courseName); XmlSerializer::readElement(elem, "TeacherName", teacherName); XmlSerializer::readElement(elem, "Score", score); } }; class Student { public: std::string name; int id; std::vector<Grade> grades; void toXml(XMLElement* elem) const { XmlSerializer::writeElement(elem, "Name", name); XmlSerializer::writeElement(elem, "Id", id); XmlSerializer::writeElementObject(elem, "Grades", grades); } void fromXml(XMLElement* elem) { XmlSerializer::readElement(elem, "Name", name); XmlSerializer::readElement(elem, "Id", id); XmlSerializer::readElementObject(elem, "Grades", grades); } }; std::string GetXmlString() { return "<Root><Name>zcc</Name><Id>4078</Id><Grades><Item><CourseName>Math</CourseName><TeacherName>bo.wang</TeacherName><Score>95</Score></Item><Item><CourseName>Physics</CourseName><TeacherName>dingtai.tang</TeacherName><Score>100</Score></Item></Grades></Root>"; /*XML文件格式 <Root> <Name>zcc</Name> <Id>4078</Id> <Grades> <Item> <CourseName>Math</CourseName> <TeacherName>bo.wang</TeacherName> <Score>95</Score> </Item> <Item> <CourseName>Physics</CourseName> <TeacherName>dingtai.tang</TeacherName> <Score>100</Score> </Item> </Grades> </Root> */ } void PrintStudentObject(const Student& student) { std::cout << "====================print student object start====================" << std::endl; std::cout << "Deserialized Student:" << std::endl; std::cout << "Name: " << student.name << std::endl; std::cout << "ID: " << student.id << std::endl; std::cout << "Grades:" << std::endl; for (const Grade& grade : student.grades) { std::cout << "Course Name: " << grade.courseName << std::endl; std::cout << "Teacher Name: " << grade.teacherName << std::endl; std::cout << "Score: " << grade.score << std::endl; std::cout << std::endl; } std::cout << "====================print student object end======================" << std::endl; } void PrintXmlString(const std::string& xml) { std::cout << "====================print xml string start========================" << std::endl; std::cout << xml << std::endl; std::cout << "====================print xml string end==========================" << std::endl; } void main() { //执行反序列化 Student student; XmlSerializer::Deserialize(GetXmlString(), student); //遍历输出Student对象 PrintStudentObject(student); //任意修改对象属性,以id为例 student.id = 363258; //执行序列化 std::string xml = XmlSerializer::Serialize(student); //输出序列化结果 PrintXmlString(xml); std::cin.get(); }
四、输出结果
五、工程获取
【点击下载】github: XmlSerialize
如有任何疑问,请在评论区留言。