c++中處理xml檔案
by xielp 發表於 2006-3-12 19:47:00
寫unmanaged code在.net時代成為一種很悲慘的事,當你需要處理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 inte***ces (c++)。什麼?windows media?呵呵,不錯,我覺得這個guide反而是最清楚的,你直接找msxml,得到的結果,我覺得還沒這個好。
在c程式裡呼叫msxml基本就是一堆com介面,不過在visual studio裡操作先要做點簡單的設定:
在你的project裡add references=>com標籤=>microsoft xml v4.0,5.0其實也有了,但因為是和office一起發布的,覺得有點怪,不想用,反正也未必用什麼很怪異的功能,4.0可以了。
然後在加入這兩行:
#i nclude
#import
標頭檔案和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(¤tnode)) && currentnode != null)
取得當前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:
....
....
具體的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, : )
#i nclude "stdafx.h"
#i nclude
#i nclude
#import
handle logfile = null;
#define indent 4
#define testhr(hr) /
void printchild(ixmldomnodelist* nodelist, int level)
else
attributes->release(); attributes = null;
} else
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)
C 中的檔案處理
1.file類 處理檔案 2.directory類 處理檔案目錄 1 如何讀取文字檔案內容 在本文介紹的程 序中,是把讀取的文字檔案,用乙個richtextbox元件顯示出來。要讀取文字檔案,必須使用到 streamreader 類,這個類是由名字空間 system.io 中定義的。通 過 stre...
C 檔案操作 提取 處理檔案中資料
在a.txt 檔案中格式為 名字 工資 將 工資提取出來,進行 4操作 再放到檔案中。string strlines file.readalllines d viang desktop a.txt encoding.default 讀取檔案的每一行 for int i 0 i strlines.le...
C 檔案處理
寫了乙個遊戲資源編輯器 中間真是挫折不少,首先是c 到c 的轉變,主要是檔案處理上的不適應,c c 程式讀寫圖形檔案相當方便 主要是win 32 api本來就是為c c 提供的 c 讀寫起來就有點不太順,我總是想用win32 api中的結構體去讀取,那樣各類資料就自動填充好了,不過c 的塊讀取只能填...