qt中tinyxml2的基本使用方法
生活随笔
收集整理的這篇文章主要介紹了
qt中tinyxml2的基本使用方法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
TinyXML是一個開源的解析XML的解析庫,能夠用于C++,能夠在Windows或Linux中編譯。
這個解析庫的模型通過解析XML文件,然后在內存中生成DOM模型,從而讓我們很方便的遍歷這棵XML樹。
DOM模型即文檔對象模型,是將整個文檔分成多個元素(如書、章、節、段等),并利用樹型結構表示這些元素之間的順序關系以及嵌套包含關系。
官方文檔,http://grinninglizard.com/tinyxml2docs/index.html,里面有例子,和下面的很類似。
這里用的是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的一些操作。
下面就編輯這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());
這個解析庫的模型通過解析XML文件,然后在內存中生成DOM模型,從而讓我們很方便的遍歷這棵XML樹。
DOM模型即文檔對象模型,是將整個文檔分成多個元素(如書、章、節、段等),并利用樹型結構表示這些元素之間的順序關系以及嵌套包含關系。
官方文檔,http://grinninglizard.com/tinyxml2docs/index.html,里面有例子,和下面的很類似。
這里用的是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的一些操作。
下面就編輯這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());
總結
以上是生活随笔為你收集整理的qt中tinyxml2的基本使用方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: QString 字符编码
- 下一篇: 天兔(Lepus)监控邮件推送安装配置