TinyXML是一个开源的解析XML的解析库,能够用于C++,能够在Windows或Linux中编译。
这个解析库的模型通过解析XML文件,然后在内存中生成DOM模型,从而让我们很方便的遍历这棵XML树。
DOM模型即文档对象模型,是将整个文档分成多个元素(如书、章、节、段等),并利用树型结构表示这些元素之间的顺序关系以及嵌套包含关系。
这个解析库的模型通过解析XML文件,然后在内存中生成DOM模型,从而让我们很方便的遍历这棵XML树。
DOM模型即文档对象模型,是将整个文档分成多个元素(如书、章、节、段等),并利用树型结构表示这些元素之间的顺序关系以及嵌套包含关系。
官方文档,http://grinninglizard.com/tinyxml2docs/index.html,里面有例子,和下面的很类似。
这里用的是TinyXML2,相比于TinyXML1,它更小,更轻量,内存的使用也更加有效。
这里用的是TinyXML2,相比于TinyXML1,它更小,更轻量,内存的使用也更加有效。
直接去官网即可下载,然后我们需要使用到的只是里面的tinyxml2.cpp和tinyxml2.h,将他们拷贝到工程的目录下面即可。
在TinyXML中,根据XML的各种元素来定义了一些类:
XmlBase:整个TinyXML模型的基类。
XmlAttribute:对应于XML中的元素的属性。
XmlNode:对应于DOM结构中的节点。
XmlComment:对应于XML中的注释。
XmlDeclaration:对应于XML中的申明部分,即<?versiong="1.0" ?>。
XmlDocument:对应于XML的整个文档。
XmlElement:对应于XML的元素。
XmlText:对应于XML的文字部分。
XmlUnknown:对应于XML的未知部分。
XmlHandler:定义了针对XML的一些操作。
XmlBase:整个TinyXML模型的基类。
XmlAttribute:对应于XML中的元素的属性。
XmlNode:对应于DOM结构中的节点。
XmlComment:对应于XML中的注释。
XmlDeclaration:对应于XML中的申明部分,即<?versiong="1.0" ?>。
XmlDocument:对应于XML的整个文档。
XmlElement:对应于XML的元素。
XmlText:对应于XML的文字部分。
XmlUnknown:对应于XML的未知部分。
XmlHandler:定义了针对XML的一些操作。
下面就编辑这xml文件
-
<?xml=version=
"1.0" encoding=
"UTF-8"?>
-
<login>
-
<userName>admin</userName>
-
<password>admin</password>
-
<ip>
172.5
.2
.21</ip>
-
<port>
37771</port>
-
<rememberPwd>
1</rememberPwd>
-
</login>
-
#ifndef _XMLLOCALCONDFIG_
-
#define _XMLLOCALCONDFIG_
-
-
#include <map>
-
#include <vector>
-
#include <string>
-
#include "tinyxml2.h"
-
-
-
#define XML_LOCALCONFIG "SwartzConfigTool.xml"
-
using
namespace tinyxml2;
-
-
typedef
struct Login_Info_t
-
{
-
std::
string strUserName;
-
std::
string strPassword;
-
std::
string strIP;
-
int nPort;
-
bool bRemPwd;
-
Login_Info_t()
-
{
-
strUserName =
"";
-
strPassword =
"";
-
strIP =
"";
-
nPort =
0;
-
bRemPwd =
false;
-
}
-
-
Login_Info_t&
operator=(Login_Info_t& src)
-
{
-
strUserName = src.strUserName;
-
strPassword = src.strPassword;
-
strIP = src.strIP;
-
nPort = src.nPort;
-
bRemPwd = src.bRemPwd;
-
return (*
this);
-
}
-
}Login_Info_t;
-
-
class XMLLocalConfig
-
{
-
public:
-
XMLLocalConfig();
-
~XMLLocalConfig();
-
-
public:
-
int LoadFile(const char *pFilename);
-
int SaveFile(const char *pFilename);
-
-
private:
-
int ParseXmlInside(XMLDocument &doc);
-
int PacketXmlInside(XMLDocument &doc, const char *pFilename);
-
-
public:
-
Login_Info_t m_loginInfo;
-
-
};
-
-
#endif //
-
#include "XMLLocalConfig.h"
-
-
//登陆信息节点
-
#define XML_TAG_LOGIN "login"
-
#define XML_TAG_USERNAME "userName"
-
#define XML_TAG_PASSWORD "password"
-
#define XML_TAG_IP "ip"
-
#define XML_TAG_PORT "port"
-
#define XML_TAG_REMEMBERPWD "rememberPwd"
-
-
-
XMLLocalConfig::XMLLocalConfig()
-
{
-
-
}
-
-
-
XMLLocalConfig::~XMLLocalConfig()
-
{
-
}
-
-
int XMLLocalConfig::LoadFile(const char *pFilename)
-
{
-
XMLDocument doc;
-
XMLError ret = doc.LoadFile(pFilename);
-
if (XML_SUCCESS != ret)
-
{
-
return
1;
-
}
-
return ParseXmlInside(doc);
-
}
-
-
int XMLLocalConfig::SaveFile(const char *pFilename)
-
{
-
XMLDocument doc;
-
int ret = PacketXmlInside(doc, pFilename);
-
return ret;
-
}
-
-
int XMLLocalConfig::ParseXmlInside(XMLDocument &doc)
-
{
-
XMLElement *rootEm = doc.RootElement();
-
XMLElement *TmpEm =
NULL;
-
TmpEm = rootEm->FirstChildElement(XML_TAG_USERNAME);
-
if (TmpEm !=
NULL)
-
m_loginInfo.strUserName = TmpEm->GetText();
-
TmpEm = rootEm->FirstChildElement(XML_TAG_PASSWORD);
-
if (TmpEm !=
NULL)
-
m_loginInfo.strPassword = TmpEm->GetText();
-
TmpEm = rootEm->FirstChildElement(XML_TAG_IP);
-
if (TmpEm !=
NULL)
-
m_loginInfo.strIP = TmpEm->GetText();
-
TmpEm = rootEm->FirstChildElement(XML_TAG_PORT);
-
if (TmpEm !=
NULL)
-
m_loginInfo.nPort = atoi(TmpEm->GetText());
-
TmpEm = rootEm->FirstChildElement(XML_TAG_REMEMBERPWD);
-
if (TmpEm !=
NULL)
-
m_loginInfo.bRemPwd = atoi(TmpEm->GetText())?
true:
false;
-
-
return
0;
-
}
-
-
int XMLLocalConfig::PacketXmlInside(XMLDocument &doc, const char *pFilename)
-
{
-
FILE* fp =
NULL;
-
fp = fopen(pFilename,
"w+");
//创建空xml文件
-
fclose(fp);
-
-
XMLDeclaration *pDecl = doc.NewDeclaration(
"xml=version=\"1.0\" encoding=\"UTF-8\"");
-
doc.LinkEndChild(pDecl);
-
-
XMLElement* loginEm = doc.NewElement(XML_TAG_LOGIN);
-
doc.InsertEndChild(loginEm);
-
-
XMLElement* UsrEm = doc.NewElement(XML_TAG_USERNAME);
-
UsrEm->LinkEndChild(doc.NewText(m_loginInfo.strUserName.c_str()));
-
loginEm->InsertEndChild(UsrEm);
-
-
XMLElement* pwdEm = doc.NewElement(XML_TAG_PASSWORD);
-
pwdEm->LinkEndChild(doc.NewText(m_loginInfo.strPassword.c_str()));
-
loginEm->InsertEndChild(pwdEm);
-
-
XMLElement* ipEm = doc.NewElement(XML_TAG_IP);
-
ipEm->LinkEndChild(doc.NewText(m_loginInfo.strIP.c_str()));
-
loginEm->InsertEndChild(ipEm);
-
-
XMLElement* portEm = doc.NewElement(XML_TAG_PORT);
-
char szPort[
10] = {
0 };
-
itoa(m_loginInfo.nPort, szPort,
10);
-
portEm->LinkEndChild(doc.NewText(szPort));
-
loginEm->InsertEndChild(portEm);
-
-
XMLElement* rwdEm = doc.NewElement(XML_TAG_REMEMBERPWD);
-
char szRwd[
10] = {
0 };
-
itoa((m_loginInfo.bRemPwd ?
1 :
0), szRwd,
10);
-
rwdEm->LinkEndChild(doc.NewText(szRwd));
-
loginEm->InsertEndChild(rwdEm);
-
-
doc.SaveFile(pFilename);
-
-
return
0;
-
}
对应读取操作
-
XMLLocalConfig XmlLocalConfig;
-
QString strXmlPath;
-
strXmlPath = QString(
"%1\\%2\\%3").arg(QDir::currentPath()).arg(
"UserData").arg(XML_LOCALCONFIG);
-
strXmlPath = QDir::toNativeSeparators(strXmlPath);
-
QByteArray byte = strXmlPath.toUtf8();
-
XmlLocalConfig.LoadFile(byte.data());
-
m_loginInfo = XmlLocalConfig.m_loginInfo;
保存操作
-
XMLLocalConfig XmlLocalConfig;
-
QString strXmlPath;
-
strXmlPath = QString(
"%1\\%2\\%3").arg(QDir::currentPath()).arg(
"UserData").arg(XML_LOCALCONFIG);
-
strXmlPath = QDir::toNativeSeparators(strXmlPath);
-
QByteArray byte = strXmlPath.toUtf8();
-
m_loginInfo.strUserName = ui.m_lineEditUserName->text().toUtf8().data();
-
m_loginInfo.strPassword = ui.m_lineEditPwd->text().toUtf8().data();
-
m_loginInfo.strIP = ui.m_lineEditAddress->text().toUtf8().data();
-
m_loginInfo.nPort = ui.m_linEditPort->text().toInt();
-
m_loginInfo.bRemPwd = ui.cbMemorizePwd->isChecked();
-
XmlLocalConfig.m_loginInfo = m_loginInfo;
-
XmlLocalConfig.SaveFile(byte.data());