TinyXML它是基于一个非常受欢迎的现在DOM型号XML解析器,简单易用且小巧玲珑,很适合存储简单数据。配置文件。

gVdn4lLDJW

当前最新版本号是2.6.2

先看一下源代码文档的结构:

gVdn4lLDJW

Docs是帮助文档。里边有许多的使用说明,只截一张图看一下:

gVdn4lLDJW

详细依据须要再看

我们使用的是它的库。能够是静态的也能够是动态库。我就用静态库了,将这里边的几个头文件和源文件一起创建一个project,生成Lib库:tinyxml.lib

gVdn4lLDJW

gVdn4lLDJW

使用的时候,将这两个头文件以及生成的静态库加进去:

gVdn4lLDJW

gVdn4lLDJW

一个简单的样例

#include

using namespace std;

#ifdef TIXML_USE_STL

#include

#include

using namespace std;

#else

#include

#endif

#if defined( WIN32 ) && defined( TUNE )

#include

_CrtMemState startMemState;

_CrtMemState endMemState;

#endif

#include "tinyxml/tinyxml.h"

int main()

{

TiXmlDocument *pDoc = new TiXmlDocument;

if (NULL==pDoc)

{

return false;

}

TiXmlDeclaration *pDeclaration = new TiXmlDeclaration("1.0","gb2312","");

if (NULL==pDeclaration)

{

return false;

}

pDoc->LinkEndChild(pDeclaration);

// 生成一个根节点

TiXmlElement *pRootEle = new TiXmlElement("索引数据包信息");

pDoc->LinkEndChild(pRootEle);

//头节点

TiXmlElement *pHeader = new TiXmlElement("头节点");

pRootEle->LinkEndChild(pHeader);

TiXmlElement *pCellNode = new TiXmlElement("字段1");

pHeader->LinkEndChild(pCellNode);

pCellNode->SetAttribute("str1","1状态");

pCellNode->SetAttribute("str2","0状态");

pDoc->SaveFile("d:\\result.xml");

return 0;

}

结果:

临时这里边的字符串不能是宽字符的。转换能够这样:

DWORD n=WideCharToMultiByte(CP_OEMCP,NULL,szBuf,-1,NULL,0,NULL,FALSE);

char *cname=new char[n+1];

WideCharToMultiByte(CP_OEMCP,NULL,szBuf,-1,cname,n,NULL,FALSE);

cname[n]=0;

当中szBuf是宽字符串。

參考:

版权声明:本文博主原创文章,博客,未经同意不得转载。

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐