溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

CMarkup類操作Xml的示例分析

發(fā)布時(shí)間:2021-09-17 14:13:52 來源:億速云 閱讀:197 作者:小新 欄目:編程語言

這篇文章給大家分享的是有關(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類操作Xml的示例分析

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ù)

ResetPosResets the current position to the start of the document
ResetMainPosResets the current main position to before the first sibling
ResetChildPosResets the current child position to before the first child

3.SavePos 與 RestorePos 復(fù)原xml Pos

SavePosSaves the current position with an optional string name using a hash map
RestorePosGoes 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ò),可以把它分享出去讓更多的人看到吧!

向AI問一下細(xì)節(jié)

免責(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)容。

xml
AI