C++中處理XML文件

一、C++中處理XML文件

FCL中的System.Xml多簡單啊,連Steve Ballmer都知道怎麼用。

事情不會總是那麼理想的,如果你要在C/C++程序裏處理XML怎麼辦呢?

選擇一:市面上的XML lib還是有幾個的,最有名的當然是libxml。我一年前用過,很不錯,我還特意寫了一份簡明教程,後來不知擱哪兒了。

選擇二:MS的MSXML,我要介紹的就是這個。

先說一下在MSDN哪裏找文檔吧,往下看的時候也好有個參考:在Index裏打:Windows Media Services 9 Series SDK=>Programming Reference=>Programming Reference (C++)=>XML DOM Interfaces (C++)。什麼?Windows Media?呵呵,不錯,我覺得這個guide反而是最清楚的,你直接找MSXML,得到的結果,我覺得還沒這個好。

在C程序裏調用MSXML基本就是一堆COM接口,不過在Visual Studio裏操作先要做點簡單的設置:

在你的Project裏Add References=>COM標籤=>Microsoft XML v4.0,5.0其實也有了,但因爲是和Office一起發佈的,覺得有點怪,不想用,反正也未必用什麼很怪異的功能,4.0可以了。

 

然後在加入這兩行:

#include <msxml2.h>
#import <msxml4.dll>

頭文件和dll庫。什麼?在哪裏加?頭文件或者c/cpp文件啊,哪裏合適放哪兒。

然後就開始編程了,先定義兩個必用的變量:

IXMLDOMDocumentPtr xmlFile = NULL;
IXMLDOMElement* xmlRoot = NULL;

爲什麼是必用的?  汗...

第一步當然是初始化COM:

if(FAILED(CoInitialize(NULL))) ....

接下來初始化xmlFile對象:

if(FAILED(xmlFile.CreateInstance("Msxml2.DOMDocument.4.0"))) ...

然後就可以加載xml文件了:

_variant_t varXml(L"C://test.xml"); //L for unicode
VARIANT_BOOL varOut;
xmlFile->load(varXml, &varOut);

取得root element:

xmlFile->get_documentElement(&xmlRoot))

取得第一級element:

IXMLDOMNodeList* xmlChildNodes = NULL;
xmlRoot->get_childNodes(&xmlChildNodes);

遍歷所有第一級element:

IXMLDOMNode* currentNode = NULL;
while(!FAILED(xmlChildNodes->nextNode(&currentNode)) && currentNode != NULL)
{
//do something
}

取得當前element的名稱:

BSTR nodeName;
currentNode->get_nodeName(&nodeName);

取得當前element的一個attribute(假設這個attribute叫type)的值:

IXMLDOMNamedNodeMap* attributes = NULL;
IXMLDOMNode* attributeName = NULL;
_bstr_t bstrAttributeName = "type";
BSTR nameVal;
currentNode->get_attributes(&attributes);
attributes->getNamedItem(bstrAttributeName, &attributeName);
attributeName->get_text(&nameVal);

需要注意的是,你要記住釋放所有的藉口,IXMLDOM***->Release(),這可不是.NET,有人幫你GC,你得自己調用Release()來減reference count,it's COM, remember?

好了,大致就這樣,順便提一下XPath:

_bstr_t bstrXmlQuery = L"/books/book[@type=scifi and @author=fox]";
IXMLDOMNodeList* nodes = NULL;
if(FAILED(xmlRoot->selectNodes(bstrXmlQuery, &nodes)) || FAILED(nodes->get_length(&length)) || length == 0)
//no match found or something went wrong
else
//match found

上面是找這樣的node:

<books>
<book type="scifi" author="fox">....
</book>
....
</books>

具體的XPath語法就查手冊吧,到處都有。

哦,對了,忘了說:如果你全部用ATL的類的話,藉口的調用會簡單一點,不過很容易轉換的,比如:

IXMLDOMDocument* 對應 IXMLDOMDocumentPtr(我這裏用了),其他基本也是加個Ptr,我不廢話了。

最後提供一個sample,我臨時攢的。工作的時候寫的程序當然不能拿來貼的,呵呵。這個sample基本就是遍歷整個xml,然後報告一遍文件的結構,對每個node,如果它有一個叫id的attribute,就同時打印id的值。If you want the complete VS project, shoot me an email. But I guess no one really needs it anyway, right, : )

#include "stdafx.h"
#include <windows.h>
#include <msxml2.h>
#import <msxml4.dll>

HANDLE logFile = NULL;

#define INDENT 4

#define TESTHR(hr) /
{ /
if(FAILED(hr)) goto fail; /
}

void PrintChild(IXMLDOMNodeList* nodeList, int level)
{
if(nodeList == NULL)
return;

IXMLDOMNode* currentNode = NULL;
IXMLDOMNodeList* childNodes = NULL;
IXMLDOMNamedNodeMap* attributes = NULL;
IXMLDOMNode* attributeID = NULL;

while(!FAILED(nodeList->nextNode(&currentNode)) && currentNode != NULL)
{
BSTR nodeName;
TESTHR(currentNode->get_nodeName(&nodeName));
DWORD dwBytesWritten;
for(int i=0; i<level*INDENT; i++)
WriteFile(logFile, L" ", (DWORD)(sizeof(WCHAR)), &dwBytesWritten, NULL);

//WCHAR msg[MAX_SIZE];
//wsprintf(msg, L"%s ", nodeName);
WriteFile(logFile, nodeName, (DWORD)(wcslen(nodeName)*sizeof(WCHAR)), &dwBytesWritten, NULL);

TESTHR(currentNode->get_attributes(&attributes));
if(attributes!=NULL)
{
_bstr_t bstrAttributeName = "id";
BSTR idVal;
TESTHR(attributes->getNamedItem(bstrAttributeName, &attributeID));
if(attributeID != NULL)
{
TESTHR(attributeID->get_text(&idVal));
WriteFile(logFile, L" ", (DWORD)(sizeof(WCHAR)), &dwBytesWritten, NULL);
WriteFile(logFile, idVal, (DWORD)(wcslen(idVal)*sizeof(WCHAR)), &dwBytesWritten, NULL);
WriteFile(logFile, L"/r/n", (DWORD)(2*sizeof(WCHAR)), &dwBytesWritten, NULL);
attributeID->Release(); attributeID = NULL;
}
else
{
WriteFile(logFile, L"/r/n", (DWORD)(2*sizeof(WCHAR)), &dwBytesWritten, NULL);
}
attributes->Release(); attributes = NULL;

}
else
{
WriteFile(logFile, L"/r/n", (DWORD)(2*sizeof(WCHAR)), &dwBytesWritten, NULL);
}

TESTHR(currentNode->get_childNodes(&childNodes));
PrintChild(childNodes, level+1);
currentNode=NULL;
}

fail:
if(childNodes!=NULL)
childNodes->Release();
if(attributeID!=NULL)
attributeID->Release();
if(attributes!=NULL)
attributes->Release();
if(currentNode != NULL)
currentNode->Release();
}

int _tmain(int argc, _TCHAR* argv[])
{

IXMLDOMDocumentPtr xmlFile = NULL;
IXMLDOMElement* xmlRoot = NULL;
_variant_t varXml(L"C://demo1.xml");

logFile = CreateFile(L"log.txt", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if(logFile == INVALID_HANDLE_VALUE)
goto fail;

TESTHR(CoInitialize(NULL));

TESTHR(xmlFile.CreateInstance("Msxml2.DOMDocument.4.0"));

VARIANT_BOOL varOut;
TESTHR(xmlFile->load(varXml, &varOut));

TESTHR(xmlFile->get_documentElement(&xmlRoot));

BSTR rootName;
DWORD dwBytesWritten;
TESTHR(xmlRoot->get_nodeName(&rootName));
WriteFile(logFile, rootName, (DWORD)(wcslen(rootName)*sizeof(WCHAR)), &dwBytesWritten, NULL);
WriteFile(logFile, L"/r/n", (DWORD)(2*sizeof(WCHAR)), &dwBytesWritten, NULL);

IXMLDOMNodeList* xmlChildNodes = NULL;
TESTHR(xmlRoot->get_childNodes(&xmlChildNodes));

PrintChild(xmlChildNodes, 2);

fail:
if(logFile != INVALID_HANDLE_VALUE)
CloseHandle(logFile);
if(xmlChildNodes!=NULL)
xmlChildNodes->Release();
if(xmlRoot!=NULL)
xmlRoot->Release();
return 0;
}

 

二、英語參考文檔

http://www.codeguru.com/Cpp/misc/misc/article.php/c3707

發佈了27 篇原創文章 · 獲贊 6 · 訪問量 13萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章