qt与tinyxml联合编译

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文件

   
   
  1. <?xml=version= "1.0" encoding= "UTF-8"?>
  2. <login>
  3. <userName>admin</userName>
  4. <password>admin</password>
  5. <ip> 172.5 .2 .21</ip>
  6. <port> 37771</port>
  7. <rememberPwd> 1</rememberPwd>
  8. </login>

 

   
   
  1. #ifndef _XMLLOCALCONDFIG_
  2. #define _XMLLOCALCONDFIG_
  3. #include <map>
  4. #include <vector>
  5. #include <string>
  6. #include "tinyxml2.h"
  7. #define XML_LOCALCONFIG "SwartzConfigTool.xml"
  8. using namespace tinyxml2;
  9. typedef struct Login_Info_t
  10. {
  11. std:: string strUserName;
  12. std:: string strPassword;
  13. std:: string strIP;
  14. int nPort;
  15. bool bRemPwd;
  16. Login_Info_t()
  17. {
  18. strUserName = "";
  19. strPassword = "";
  20. strIP = "";
  21. nPort = 0;
  22. bRemPwd = false;
  23. }
  24. Login_Info_t& operator=(Login_Info_t& src)
  25. {
  26. strUserName = src.strUserName;
  27. strPassword = src.strPassword;
  28. strIP = src.strIP;
  29. nPort = src.nPort;
  30. bRemPwd = src.bRemPwd;
  31. return (* this);
  32. }
  33. }Login_Info_t;
  34. class XMLLocalConfig
  35. {
  36. public:
  37. XMLLocalConfig();
  38. ~XMLLocalConfig();
  39. public:
  40. int LoadFile(const char *pFilename);
  41. int SaveFile(const char *pFilename);
  42. private:
  43. int ParseXmlInside(XMLDocument &doc);
  44. int PacketXmlInside(XMLDocument &doc, const char *pFilename);
  45. public:
  46. Login_Info_t m_loginInfo;
  47. };
  48. #endif //


   
   
  1. #include "XMLLocalConfig.h"
  2. //登陆信息节点
  3. #define XML_TAG_LOGIN "login"
  4. #define XML_TAG_USERNAME "userName"
  5. #define XML_TAG_PASSWORD "password"
  6. #define XML_TAG_IP "ip"
  7. #define XML_TAG_PORT "port"
  8. #define XML_TAG_REMEMBERPWD "rememberPwd"
  9. XMLLocalConfig::XMLLocalConfig()
  10. {
  11. }
  12. XMLLocalConfig::~XMLLocalConfig()
  13. {
  14. }
  15. int XMLLocalConfig::LoadFile(const char *pFilename)
  16. {
  17. XMLDocument doc;
  18. XMLError ret = doc.LoadFile(pFilename);
  19. if (XML_SUCCESS != ret)
  20. {
  21. return 1;
  22. }
  23. return ParseXmlInside(doc);
  24. }
  25. int XMLLocalConfig::SaveFile(const char *pFilename)
  26. {
  27. XMLDocument doc;
  28. int ret = PacketXmlInside(doc, pFilename);
  29. return ret;
  30. }
  31. int XMLLocalConfig::ParseXmlInside(XMLDocument &doc)
  32. {
  33. XMLElement *rootEm = doc.RootElement();
  34. XMLElement *TmpEm = NULL;
  35. TmpEm = rootEm->FirstChildElement(XML_TAG_USERNAME);
  36. if (TmpEm != NULL)
  37. m_loginInfo.strUserName = TmpEm->GetText();
  38. TmpEm = rootEm->FirstChildElement(XML_TAG_PASSWORD);
  39. if (TmpEm != NULL)
  40. m_loginInfo.strPassword = TmpEm->GetText();
  41. TmpEm = rootEm->FirstChildElement(XML_TAG_IP);
  42. if (TmpEm != NULL)
  43. m_loginInfo.strIP = TmpEm->GetText();
  44. TmpEm = rootEm->FirstChildElement(XML_TAG_PORT);
  45. if (TmpEm != NULL)
  46. m_loginInfo.nPort = atoi(TmpEm->GetText());
  47. TmpEm = rootEm->FirstChildElement(XML_TAG_REMEMBERPWD);
  48. if (TmpEm != NULL)
  49. m_loginInfo.bRemPwd = atoi(TmpEm->GetText())? true: false;
  50. return 0;
  51. }
  52. int XMLLocalConfig::PacketXmlInside(XMLDocument &doc, const char *pFilename)
  53. {
  54. FILE* fp = NULL;
  55. fp = fopen(pFilename, "w+"); //创建空xml文件
  56. fclose(fp);
  57. XMLDeclaration *pDecl = doc.NewDeclaration( "xml=version=\"1.0\" encoding=\"UTF-8\"");
  58. doc.LinkEndChild(pDecl);
  59. XMLElement* loginEm = doc.NewElement(XML_TAG_LOGIN);
  60. doc.InsertEndChild(loginEm);
  61. XMLElement* UsrEm = doc.NewElement(XML_TAG_USERNAME);
  62. UsrEm->LinkEndChild(doc.NewText(m_loginInfo.strUserName.c_str()));
  63. loginEm->InsertEndChild(UsrEm);
  64. XMLElement* pwdEm = doc.NewElement(XML_TAG_PASSWORD);
  65. pwdEm->LinkEndChild(doc.NewText(m_loginInfo.strPassword.c_str()));
  66. loginEm->InsertEndChild(pwdEm);
  67. XMLElement* ipEm = doc.NewElement(XML_TAG_IP);
  68. ipEm->LinkEndChild(doc.NewText(m_loginInfo.strIP.c_str()));
  69. loginEm->InsertEndChild(ipEm);
  70. XMLElement* portEm = doc.NewElement(XML_TAG_PORT);
  71. char szPort[ 10] = { 0 };
  72. itoa(m_loginInfo.nPort, szPort, 10);
  73. portEm->LinkEndChild(doc.NewText(szPort));
  74. loginEm->InsertEndChild(portEm);
  75. XMLElement* rwdEm = doc.NewElement(XML_TAG_REMEMBERPWD);
  76. char szRwd[ 10] = { 0 };
  77. itoa((m_loginInfo.bRemPwd ? 1 : 0), szRwd, 10);
  78. rwdEm->LinkEndChild(doc.NewText(szRwd));
  79. loginEm->InsertEndChild(rwdEm);
  80. doc.SaveFile(pFilename);
  81. return 0;
  82. }

对应读取操作

    
    
  1. XMLLocalConfig XmlLocalConfig;
  2. QString strXmlPath;
  3. strXmlPath = QString( "%1\\%2\\%3").arg(QDir::currentPath()).arg( "UserData").arg(XML_LOCALCONFIG);
  4. strXmlPath = QDir::toNativeSeparators(strXmlPath);
  5. QByteArray byte = strXmlPath.toUtf8();
  6. XmlLocalConfig.LoadFile(byte.data());
  7. m_loginInfo = XmlLocalConfig.m_loginInfo;

保存操作

      
      
  1. XMLLocalConfig XmlLocalConfig;
  2. QString strXmlPath;
  3. strXmlPath = QString( "%1\\%2\\%3").arg(QDir::currentPath()).arg( "UserData").arg(XML_LOCALCONFIG);
  4. strXmlPath = QDir::toNativeSeparators(strXmlPath);
  5. QByteArray byte = strXmlPath.toUtf8();
  6. m_loginInfo.strUserName = ui.m_lineEditUserName->text().toUtf8().data();
  7. m_loginInfo.strPassword = ui.m_lineEditPwd->text().toUtf8().data();
  8. m_loginInfo.strIP = ui.m_lineEditAddress->text().toUtf8().data();
  9. m_loginInfo.nPort = ui.m_linEditPort->text().toInt();
  10. m_loginInfo.bRemPwd = ui.cbMemorizePwd->isChecked();
  11. XmlLocalConfig.m_loginInfo = m_loginInfo;
  12. XmlLocalConfig.SaveFile(byte.data());




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值