新聞中心
這篇文章給大家分享的是有關(guān)CMarkup類(lèi)操作Xml的示例分析的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。
專(zhuān)業(yè)從事企業(yè)網(wǎng)站建設(shè)和網(wǎng)站設(shè)計(jì)服務(wù),包括網(wǎng)站建設(shè)、國(guó)際域名空間、雅安服務(wù)器托管、企業(yè)郵箱、微信公眾號(hào)開(kāi)發(fā)、微信支付寶小程序開(kāi)發(fā)、app軟件定制開(kāi)發(fā)、軟件開(kāi)發(fā)、等服務(wù)。公司始終通過(guò)不懈的努力和以更高的目標(biāo)來(lái)要求自己,在不斷完善自身管理模式和提高技術(shù)研發(fā)能力的同時(shí),大力倡導(dǎo)推行新經(jīng)濟(jì)品牌戰(zhàn)略,促進(jìn)互聯(lián)網(wǎng)事業(yè)的發(fā)展。
一、下載Markup.cpp 和 Markup.h
二、將此兩個(gè)文件放置于工程目錄下
三、在需要用到CMarkup的地方 #include "Markup.h"
當(dāng)然VC中還需要配置一下環(huán)境
在VC6.0下:
a.在Markup.cpp的頂端加上 #include"stdafx.h" 或者
b.關(guān)閉Markup.cpp的預(yù)編譯頭設(shè)置,具體方法如下:
Project->Setting 彈出ProjectSetting對(duì)話框,在左邊的文件樹(shù)下選擇Markup.cpp,然后在 "Settings for" 下拉框下選擇 "All Configurations",選擇 C/C++標(biāo)簽頁(yè),
接著在Category下拉框下選中 "Precompiled Headers"選項(xiàng),選中下面的"Not Using Precompiled Headers." 單選按鈕即可
CMarkup的基本使用:
例如要讀取如下UserInfos.xml的內(nèi)容
WangYao 25 Hisin 27
源代碼如下:
CMarkup xml; bool flag; //加載Xml文件 flag = xml.Load("d:\\UserInfos.xml"); if (!flag) { AfxMessageBox(TEXT("加載d:\\UserInfos.xml失敗,請(qǐng)檢查")); return; } //定位到Root Elem xml.ResetPos(); flag = xml.FindElem("UserInfos"); //Root Elem為if (!flag) { return; } xml.IntoElem(); //進(jìn)入根節(jié)點(diǎn) while(xml.FindElem(TEXT("UserInfo"))) { xml.IntoElem(); //進(jìn)入 //獲取name節(jié)點(diǎn)數(shù)據(jù) flag = xml.FindElem(TEXT("name")); if (flag) { CString cstrName; cstrName = xml.GetData(); AfxMessageBox(cstrName); } //獲取age節(jié)點(diǎn)數(shù)據(jù) xml.ResetMainPos(); //保證不管name節(jié)點(diǎn)和age節(jié)點(diǎn)的順序如何,都能找到age節(jié)點(diǎn) flag = xml.FindElem(TEXT("age")); if (flag) { CString cstrAge; cstrAge = xml.GetData(); AfxMessageBox(cstrAge); } xml.OutOfElem(); //跳出 } xml.OutOfElem(); //跳出根節(jié)點(diǎn)
當(dāng)然實(shí)現(xiàn)同樣的功能也可以不進(jìn)入U(xiǎn)serInfo節(jié)點(diǎn),源碼如下,請(qǐng)仔細(xì)對(duì)比
xml.IntoElem(); //進(jìn)入根節(jié)點(diǎn) while(xml.FindElem(TEXT("UserInfo"))) { //獲取name子節(jié)點(diǎn)數(shù)據(jù) flag = xml.FindChildElem(TEXT("name")); if (flag) { CString cstrName; cstrName = xml.GetChildData(); AfxMessageBox(cstrName); } //獲取age子節(jié)點(diǎn)數(shù)據(jù) xml.ResetChildPos(); //保證不管name子節(jié)點(diǎn)和age子節(jié)點(diǎn)的順序如何,都能找到age子節(jié)點(diǎn) flag = xml.FindChildElem(TEXT("age")); if (flag) { CString cstrAge; cstrAge = xml.GetChildData(); AfxMessageBox(cstrAge); } } xml.OutOfElem(); //跳出根節(jié)點(diǎn)
Tips:
1.IntoElem與OutOfElem方法應(yīng)成對(duì)使用
2.關(guān)于重置xml的Pos的函數(shù)
ResetPos | Resets the current position to the start of the document |
ResetMainPos | Resets the current main position to before the first sibling |
ResetChildPos | Resets the current child position to before the first child |
3.SavePos 與 RestorePos 復(fù)原xml Pos
SavePos | Saves the current position with an optional string name using a hash map |
RestorePos | Goes to the position saved with SavePos
|
比如:
... xml.SavePos(Text("abc")); OpXml(xml); //該函數(shù)可能會(huì)改變xml的Pos,則可以利用SavePos與RestorePos復(fù)原該函數(shù)執(zhí)行前xml的Pos xml.RestorePos(Text("abc")); ...
感謝各位的閱讀!關(guān)于“CMarkup類(lèi)操作Xml的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
網(wǎng)頁(yè)名稱(chēng):CMarkup類(lèi)操作Xml的示例分析
文章起源:http://www.ef60e0e.cn/article/gsdedc.html