您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關(guān)CMarkup類操作Xml的示例分析的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。
一、下載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對話框,在左邊的文件樹下選擇Markup.cpp,然后在 "Settings for" 下拉框下選擇 "All Configurations",選擇 C/C++標(biāo)簽頁,
接著在Category下拉框下選中 "Precompiled Headers"選項(xiàng),選中下面的"Not Using Precompiled Headers." 單選按鈕即可
CMarkup的基本使用:
例如要讀取如下UserInfos.xml的內(nèi)容
<?xml version="1.0" encoding="UTF-8" ?> <UserInfos> <UserInfo> <name>WangYao</name> <age>25</age> </UserInfo> <UserInfo> <name>Hisin</name> <age>27</age> </UserInfo> </UserInfos>
源代碼如下:
CMarkup xml; bool flag; //加載Xml文件 flag = xml.Load("d:\\UserInfos.xml"); if (!flag) { AfxMessageBox(TEXT("加載d:\\UserInfos.xml失敗,請檢查")); return; } //定位到Root Elem xml.ResetPos(); flag = xml.FindElem("UserInfos"); //Root Elem為<UserInfos></UserInfos> if (!flag) { return; } xml.IntoElem(); //進(jìn)入根節(jié)點(diǎn) while(xml.FindElem(TEXT("UserInfo"))) { xml.IntoElem(); //進(jìn)入<UserInfo></UserInfo> //獲取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(); //跳出<UserInfo></UserInfo> } xml.OutOfElem(); //跳出根節(jié)點(diǎn)
當(dāng)然實(shí)現(xiàn)同樣的功能也可以不進(jìn)入U(xiǎn)serInfo節(jié)點(diǎn),源碼如下,請仔細(xì)對比
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)成對使用
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類操作Xml的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。