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

该项目属于开源项目,当前最新版本号是2.6.2

 

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

 

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

 

详细依据须要再看

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

 

 

 

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

 

 

 

 

一个简单的样例


#include <iostream>
using namespace std;

#ifdef TIXML_USE_STL
#include <iostream>
#include <sstream>
using namespace std;
#else
#include <stdio.h>
#endif

#if defined( WIN32 ) && defined( TUNE )
#include <crtdbg.h>
_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是宽字符串。