寫Unmanaged Code在.NET時代成為一種很悲慘的事,當(dāng)你需要處理XML文件時,這種感覺會變得尤其強(qiáng)烈。FCL中的System.Xml多簡單啊,連Steve Ballmer都知道怎么用。
事情不會總是那么理想的,如果你要在C/C++程序里處理XML怎么辦呢?
選擇一:市面上的XML lib還是有幾個的,最有名的當(dāng)然是libxml。我一年前用過,很不錯,我還特意寫了一份簡明教程,后來不知擱哪兒了。
選擇二:MS的MSXML,我要介紹的就是這個。
先說一下在MSDN哪里找文檔吧,往下看的時候也好有個參考:在Index里打:Windows Media Services 9 Series SDK=>Programming Reference=>Programming Reference (C++)=>XML DOM Interfaces (C++)。什么?Windows Media?呵呵,不錯,我覺得這個guide反而是最清楚的,你直接找MSXML,得到的結(jié)果,我覺得還沒這個好。
在C程序里調(diào)用MSXML基本就是一堆COM接口,不過在Visual Studio里操作先要做點簡單的設(shè)置:
在你的Project里Add References=>COM標(biāo)簽=>Microsoft XML v4.0,5.0其實也有了,但因為是和Office一起發(fā)布的,覺得有點怪,不想用,反正也未必用什么很怪異的功能,4.0可以了。
然后在加入這兩行:
#include <msxml2.h>
#import <msxml4.dll>
頭文件和dll庫。什么?在哪里加?頭文件或者c/cpp文件啊,哪里合適放哪兒。
然后就開始編程了,先定義兩個必用的變量:
IXMLDOMDocumentPtr xmlFile = NULL;
IXMLDOMElement* xmlRoot = NULL;
為什么是必用的? 汗...
第一步當(dāng)然是初始化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(¤tNode)) && currentNode != NULL)
{
//do something
}
取得當(dāng)前element的名稱:
BSTR nodeName;
currentNode->get_nodeName(&nodeName);
取得當(dāng)前element的一個attribute(假設(shè)這個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,你得自己調(diào)用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的類的話,借口的調(diào)用會簡單一點,不過很容易轉(zhuǎn)換的,比如:
IXMLDOMDocument* 對應(yīng) IXMLDOMDocumentPtr(我這里用了),其他基本也是加個Ptr,我不廢話了。
最后提供一個sample,我臨時攢的。工作的時候?qū)懙某绦虍?dāng)然不能拿來貼的,呵呵。這個sample基本就是遍歷整個xml,然后報告一遍文件的結(jié)構(gòu),對每個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(¤tNode)) && 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;
}