本人已经将代码和笔记上传到个人gitee仓库:https://gitee.com/Aurora811/Using-class.git
运行环境:VS 2017
1、加载类
TinyXML2
是简单实用的开源的C++XML文件解析库,可以很方便的应用到现有的项目之中。自己对tinyth的读写操作进行了封装XmlFile类。
在新建的项目中加载tinyxml2.h、tinyxml2.cpp、XmlFile.h和XmlFile.cpp。
2、开始调试
遇到问题一:
解决方式是:
将项目的字符集改为使用多字节字符集
遇到问题二:
fatal error C1010: 在查找预编译头时遇到意外的文件结尾。是否忘记了向源中添加“#include “pch.h””?
解决方式是:
在tinyxnl2.cpp中添加#include "pch.h"头文件
3、读写XMl操作
使用已经封装好的XmlFile类,来进行创建、打开、关闭、读Xml文件(获取根节点)和写Xml文件(插入注释、插入根节点、插入子节点)
读过程实例:
XmlFile xmlfile;
if (xmlfile.Open(Path))
{
//AfxMessageBox(_T("xml文件加载成功!"));
//获取根节点
tinyxml2::XMLElement * root = xmlfile.GetRootNode();
//读取根节点
CString strIP = root->Attribute("IP");
CString struser = root->Attribute("user");
CString strpassword = root->Attribute("password");
CString strdatabase = root->Attribute("database");
//连接MySQL数据库
MySQlConnet(strIP,struser,strpassword,strdatabase);
//读取表名
tinyxml2::XMLElement * TableNode = root->FirstChildElement("TableInfo");
CString strTableName = TableNode->Attribute("TableName");//表名
//读取字段名
tinyxml2::XMLElement* pNode = NULL;
pNode = TableNode->FirstChildElement("Filed");
while (NULL != pNode)
{
//读取列
CString strNum = pNode->Attribute("Name");
CString strMoney = pNode->Attribute("Money");
CString strDate = pNode->Attribute("Date");
//将数据保存到Map容器中
//m_mapDbInfo.SetAt(strNum, strMoney);
//指向下一个字段
pNode = pNode->NextSiblingElement();
}
}
else
{
AfxMessageBox(_T("xml文件加载失败!"));
}