資源簡介
c++讀取XML配置文件C++中并沒有操作XML文件的標準庫,因此大家需要使用各自熟悉的XML庫來解決XML文件的讀取與寫入。XML的一個重要用途是作為程序的配置文件,存儲程序運行相關的各種數據。本文總結了使用libxml2庫來對XML配置文件進行編程的一些經驗。最后提供了一個封裝好的類CXMLConfig,并詳細說明了該類的功能、使用方法和注意事項。

代碼片段和文件信息
#include?“stdafx.h“
#include?
#include?l/parser.h>
#include?l/xpath.h>
#include?
#include?“Code_Conv.h“
//代碼轉換:從一種編碼轉為另一種編碼???
int?code_convert(char*?from_charset?char*?to_charset?char*?inbuf
?int?inlen?char*?outbuf?int?outlen)
{
iconv_t?cd;
char**?pin?=?&inbuf;???
char**?pout?=?&outbuf;
cd?=?iconv_open(to_charsetfrom_charset);???
if(cd?==?0)
return?-1;
memset(outbuf0outlen);???
if(iconv(cd(const?char**)pin(unsigned?int?*)&inlenpout(unsigned?int*)&outlen)
==?-1)
return?-1;???
iconv_close(cd);
return?0;???
}
//UNICODE碼轉為GB2312碼???
//成功則返回一個動態分配的char*變量,需要在使用完畢后手動free,失敗返回NULL
char*?u2g(char?*inbuf)???
{
int?nOutLen?=?2?*?strlen(inbuf)?+?1;
char*?szOut?=?(char*)malloc(nOutLen);
if?(-1?==?code_convert(“utf-8““gb2312“inbufstrlen(inbuf)szOutnOutLen))
{
free(szOut);
szOut?=?NULL;
}
return?szOut;
}???
//GB2312碼轉為UNICODE碼???
//成功則返回一個動態分配的char*變量,需要在使用完畢后手動free,失敗返回NULL
char*?g2u(char?*inbuf)???
{
int?nOutLen?=?2?*?strlen(inbuf)?+?1;
char*?szOut?=?(char*)malloc(nOutLen);
if?(-1?==?code_convert(“gb2312““utf-8“inbufstrlen(inbuf)szOutnOutLen))
{
free(szOut);
szOut?=?NULL;
}
return?szOut;
}???
xmlXPathobjectPtr?get_nodeset(xmlDocPtr?doc?const?xmlChar?*xpath)?
{
xmlXPathContextPtr?context;
xmlXPathobjectPtr?result;
context?=?xmlXPathNewContext(doc);
if?(context?==?NULL)?
{
printf(“context?is?NULL\n“);
return?NULL;?
}
result?=?xmlXPathEvalexpression(xpath?context);
xmlXPathFreeContext(context);
if?(result?==?NULL)?
{
printf(“xmlXPathEvalexpression?return?NULL\n“);?
return?NULL;??
}
if?(xmlXPathNodeSetIsEmpty(result->nodesetval))?
{
xmlXPathFreeobject(result);
printf(“nodeset?is?empty\n“);
return?NULL;
}
return?result;
}
string?getxmlString(xmlDocPtr?doc?const?char?*szXpath)
{
xmlXPathobjectPtr?result;
string?strRel;
result?=?get_nodeset(docBAD_CAST?szXpath);
if?(result?==?NULL)?
{
printf(“xmlXPathEvalexpression?return?NULL\n“);?
return?NULL;??
}
xmlNodeSetPtr?nodeSetPtr?=?result->nodesetval;
//查出符合xpath的節點集合
if?(xmlXPathNodeSetIsEmpty(nodeSetPtr))?
{
xmlXPathFreeobject(result);
printf(“nodeset?is?empty\n“);
return?NULL;
}
//查出節點集合中唯一節點的內容值
xmlNodePtr?nodePtr?=?nodeSetPtr->nodeTab[0];????
strRel?=?(const?char*)xmlNodeGetContent(nodePtr);
return?strRel;
}
int?getxmlInt(xmlDocPtr?doc?const?char*?szXpath)
{
xmlXPathobjectPtr?result;
int?iRel;
result?=?get_nodeset(docBAD_CAST?szXpath);
if?(result?==?NULL)?
{
printf(“xmlXPathEvalexpression?return?NULL\n“);?
return?NULL;??
}
xmlNodeSetPtr?nodeSetPtr?=?result->nodesetval;
//查出符合xpath的節點集合
if?(xmlXPathNodeSetIsEmpty(nodeSetPtr))?
{
xmlXPathFreeobject(result);
printf(“nodeset?is?empty\n“);
return?NULL;
}
xmlNodePtr?nodePtr?=?nodeSetPtr->nodeTab[0];????
iRel?=?atoi((const?cha
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????4056??2008-05-27?09:44??xm
?????文件????????930??2008-05-27?09:44??xm
?????文件?????????62??2008-05-26?14:59??xm
?????文件?????888832??2006-05-03?21:57??xm
?????文件?????967168??2007-08-27?20:11??xm
?????文件????????162??2008-05-27?15:03??xm
?????文件????????162??2008-05-27?15:03??xm
?????文件??????55808??2007-07-02?17:06??xm
?????文件??????14149??2008-05-27?16:20??xm
?????文件?????214152??2008-05-26?15:48??xm
?????文件?????508928??2008-05-27?16:20??xm
?????文件???????2508??2008-05-26?15:48??xm
?????文件??????66560??2008-05-27?16:20??xm
?????文件?????110592??2008-05-27?16:20??xm
?????文件???????1146??2008-05-27?15:32??xm
?????文件???????4639??2008-05-26?15:50??xm
?????文件???????1379??2008-05-27?16:20??xm
?????文件???????1220??2008-05-26?15:48??xm
?????文件????????295??2008-05-26?15:48??xm
?????文件????????769??2008-05-26?15:48??xm
?????文件???????2508??2008-05-27?10:42??xm
?????文件??????44244??2008-05-27?14:30??xm
?????文件?????214152??2008-05-27?10:42??xm
?????文件?????582656??2008-05-27?14:31??xm
?????文件?????123904??2008-05-27?14:31??xm
?????文件?????176128??2008-05-27?14:31??xm
?????文件??????67456??2008-05-27?14:31??xm
?????文件???????1220??2008-05-27?10:37??xm
?????文件????????295??2008-05-27?10:37??xm
?????文件????????769??2008-05-27?10:37??xm
............此處省略33個文件信息
評論
共有 條評論