程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C >> C語言基礎知識 >> VC解析XML文件-CMarkup的使用詳解

VC解析XML文件-CMarkup的使用詳解

編輯:C語言基礎知識
VC解析XML文件的工具有很多,CMarkup, tinyXML,還有IBM的,MS的等等。
據說tinyXML很好,可能字符集問題,我編譯不了。
所以就用CMarkup來解析,使用過後,覺得非常不錯,使用起來很方便。
CMarkup下載地址:http://www.firstobject.com/
發現網上有方法很法,我就摘下來了
代碼如下:

//----------UserInfo.xml--------------   
xml version="1.0" encoding="UTF-8" ?>    
<UserInfo>   
  <UserID>luo</UserID>    
  <UserID>lin</UserID>    
  </UserInfo>  
//----------UserInfo.xml--------------
xml version="1.0" encoding="UTF-8" ?>
<UserInfo>
<UserID>luo</UserID>
<UserID>lin</UserID>
</UserInfo>

一、生成UserInfo.xml
代碼如下:

CMarkup xml;    
xml.SetDoc("xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n");    
xml.AddElem("UserInfo");    
xml.IntoElem();    
xml.AddElem("UserID","luo");    
xml.AddElem("UserID","lin");    
xml.OutOfElem();    
xml.Save("UserInfo.xml");   
CMarkup xml;
xml.SetDoc("xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n");
xml.AddElem("UserInfo");
xml.IntoElem();
xml.AddElem("UserID","luo");
xml.AddElem("UserID","lin");
xml.OutOfElem();
xml.Save("UserInfo.xml");

二、浏覽特定元素
代碼如下:

//----查看所有UserID----
CMarkup xml;    
xml.Load("UserInfo.xml");    
BOOL bFind = true;    
xml.ResetMainPos();    
while (xml.FindChildElem("UserID");)    
{    
      //此時接點還是父接點    
        CString strTagName = _T("");    
        CString strData = _T("");    
        strTagName = xml.GetChildTagName();    
        strData = xml.GetChildData();    
        TRACE("\n---tagName:%s,Data:%s--\n",strTagName,strData);    
}    
      /********************這樣也可以******************   
      CMarkup xml;   
      xml.Load("UserInfo.xml");   
      BOOL bFind = true;   
      xml.ResetMainPos();   

      while (xml.FindChildElem("UserID");)   
      {   
                    xml.IntoElem();   
                    CString strTagName = _T("");   
                    CString strData = _T("");   
                    strTagName = xml.GetTagName();   
                    strData = xml.GetData();   
                    TRACE("\n---tagName:%s,Data:%s--\n",strTagName,strData);   
                    xml.OutOfElem();   
      }*/    
      /**************這樣也可以**********************   
      CMarkup xml;   
      xml.Load("UserInfo.xml");   
      BOOL bFind = true;   
      xml.ResetMainPos();   
      xml.FindElem();  //UserInfo   
      xml.IntoElem();   
      while (xml.FindElem("UserID");)   
      {   
                    CString strTagName = _T("");   
                    CString strData = _T("");   
                    strTagName = xml.GetTagName();   
                    strData = xml.GetData();   
                    TRACE("\n---tagName:%s,Data:%s--\n",strTagName,strData);   
      }   
      */    

代碼如下:

//-------------結果----------    
---tagName:UserID,Data:luo--    
---tagName:UserID,Data:lin—   
//----查看所有UserID----
CMarkup xml;
xml.Load("UserInfo.xml");
BOOL bFind = true;
xml.ResetMainPos();
while (xml.FindChildElem("UserID");)
{
      //此時接點還是父接點
        CString strTagName = _T("");
        CString strData = _T("");
        strTagName = xml.GetChildTagName();
        strData = xml.GetChildData();
        TRACE("\n---tagName:%s,Data:%s--\n",strTagName,strData);
}
      /********************這樣也可以******************
      CMarkup xml;
      xml.Load("UserInfo.xml");
      BOOL bFind = true;
      xml.ResetMainPos();

      while (xml.FindChildElem("UserID");)
      {
                    xml.IntoElem();
                    CString strTagName = _T("");
                    CString strData = _T("");
                    strTagName = xml.GetTagName();
                    strData = xml.GetData();
                    TRACE("\n---tagName:%s,Data:%s--\n",strTagName,strData);
                    xml.OutOfElem();
      }*/
      /**************這樣也可以**********************
      CMarkup xml;
      xml.Load("UserInfo.xml");
      BOOL bFind = true;
      xml.ResetMainPos();
      xml.FindElem();  //UserInfo
      xml.IntoElem();
      while (xml.FindElem("UserID");)
      {
                    CString strTagName = _T("");
                    CString strData = _T("");
                    strTagName = xml.GetTagName();
                    strData = xml.GetData();
                    TRACE("\n---tagName:%s,Data:%s--\n",strTagName,strData);
      }
      */
//-------------結果----------
---tagName:UserID,Data:luo--
---tagName:UserID,Data:lin—

三、修改
代碼如下:

//--------把UserID為”luo”改為”flypigluo”-----------
BOOL bLoadXml = false;    
CMarkup xml;    
bLoadXml = xml.Load("UserInfo.xml");    
      if (bLoadXml)    
      {    
              CString strUserID = _T("");    
              xml.ResetMainPos();    
              xml.FindElem();    
              xml.IntoElem();    
              while (xml.FindElem("UserID"))    
              {    
                    strUserID = xml.GetData();    
                    if (strUserID=="luo")    
                    {    
                            xml.SetData(CString("flypig")+strUserID);    
                            xml.Save("UserInfo.xml");    
                            break;    
                    }    
              }    
      }   
//--------把UserID為”luo”改為”flypigluo”-----------
BOOL bLoadXml = false;
CMarkup xml;
bLoadXml = xml.Load("UserInfo.xml");
      if (bLoadXml)
      {
              CString strUserID = _T("");
              xml.ResetMainPos();
              xml.FindElem();
              xml.IntoElem();
              while (xml.FindElem("UserID"))
              {
                    strUserID = xml.GetData();
                    if (strUserID=="luo")
                    {
                            xml.SetData(CString("flypig")+strUserID);
                            xml.Save("UserInfo.xml");
                            break;
                    }
              }
      }

四、添加
1  添在最後面(使用的是AddElem)
代碼如下:

//加在最後面
BOOL bLoadXml = false;    
      CMarkup xml;    
      bLoadXml = xml.Load("UserInfo.xml");    
      if (bLoadXml)    
      {    
              xml.ResetMainPos();    
              xml.FindElem();    
              xml.IntoElem();    
              xml.AddElem("UserID","luoluo");    
              xml.OutOfElem();    
              xml.Save("UserInfo.xml");    
      }    
//--------原來為------    
xml version="1.0" encoding="UTF-8" ?>    
<UserInfo>    
  <UserID>luo</UserID>    
  <UserID>lin</UserID>    
  </UserInfo>    
//---------添加後為-------------    
xml version="1.0" encoding="UTF-8" ?>    
<UserInfo>    
  <UserID>luo</UserID>    
  <UserID>lin</UserID>    
  <UserID>luoluo</UserID>    
  </UserInfo>

2  加在最前面(使用InsertElem)
代碼如下:

BOOL bLoadXml = false;    
CMarkup xml;    
bLoadXml = xml.Load("UserInfo.xml");    
if (bLoadXml)    
{    
      xml.ResetMainPos();    
      xml.FindElem();    
      xml.IntoElem();    
      xml.InsertElem("UserID","AddUserIDHead");    
      xml.OutOfElem();    
      xml.Save("UserInfo.xml");    
}    
//----原來為—    
xml version="1.0" encoding="UTF-8" ?>    
<UserInfo>    
  <UserID>luo</UserID>    
  <UserID>lin</UserID>    
  </UserInfo>    
//------修改後-----    
xml version="1.0" encoding="UTF-8" ?>    
<UserInfo>    
  <UserID>AddUserIDHead</UserID>    
  <UserID>luo</UserID>    
  <UserID>lin</UserID>    
  </UserInfo>   
BOOL bLoadXml = false;
CMarkup xml;
bLoadXml = xml.Load("UserInfo.xml");
if (bLoadXml)
{
      xml.ResetMainPos();
      xml.FindElem();
      xml.IntoElem();
      xml.InsertElem("UserID","AddUserIDHead");
      xml.OutOfElem();
      xml.Save("UserInfo.xml");
}
//----原來為—
xml version="1.0" encoding="UTF-8" ?>
<UserInfo>
  <UserID>luo</UserID>
  <UserID>lin</UserID>
  </UserInfo>
//------修改後-----
xml version="1.0" encoding="UTF-8" ?>
<UserInfo>
  <UserID>AddUserIDHead</UserID>
  <UserID>luo</UserID>
  <UserID>lin</UserID>
  </UserInfo>

五、刪除
代碼如下:

CMarkup xml;    
xml.Load("UserInfo.xml");    
BOOL bFind = true;    
xml.ResetMainPos();         
while (bFind)    
{    
      bFind = xml.FindChildElem("UserID");    
      if (bFind)    
      {    
              //此時接點還是父接點    
              CString strData = _T("");    
              strData = xml.GetChildData();    
              if (strData=="luo")    
              {    
                    xml.RemoveChildElem();    
                    xml.Save("UserInfo.xml");    
                    break;    
              }    
      }    
}   
CMarkup xml;
xml.Load("UserInfo.xml");
BOOL bFind = true;
xml.ResetMainPos();     
while (bFind)
{
      bFind = xml.FindChildElem("UserID");
      if (bFind)
      {
              //此時接點還是父接點
              CString strData = _T("");
              strData = xml.GetChildData();
              if (strData=="luo")
              {
                    xml.RemoveChildElem();
                    xml.Save("UserInfo.xml");
                    break;
              }
      }
}

六、查找
見第二點浏覽。類似上面的例子裡面沒有屬性這些東西,下面的一個例子是有屬性的
代碼如下:

//UserInfo1.xml    
xml version="1.0" encoding="UTF-8" ?>    
<UserInfo>    
  <User UserID="UserID00" UserPwd="UserPwd00" UserLevel="158" />    
  <User UserID="UserID01" UserPwd="UserPwd01" UserLevel="162" />    
  <User UserID="UserID02" UserPwd="UserPwd02" UserLevel="165" />    
  <User UserID="UserID03" UserPwd="UserPwd03" UserLevel="168" />    
  <User UserID="UserID04" UserPwd="UserPwd04" UserLevel="171" />    
  <User UserID="UserID05" UserPwd="UserPwd05" UserLevel="175" />    
  <User UserID="UserID06" UserPwd="UserPwd06" UserLevel="178" />    
  <User UserID="UserID07" UserPwd="UserPwd07" UserLevel="181" />    
  <User UserID="UserID08" UserPwd="UserPwd08" UserLevel="184" />    
  <User UserID="UserID09" UserPwd="UserPwd09" UserLevel="188" />    
  </UserInfo>   
//UserInfo1.xml
xml version="1.0" encoding="UTF-8" ?>
<UserInfo>
  <User UserID="UserID00" UserPwd="UserPwd00" UserLevel="158" />
  <User UserID="UserID01" UserPwd="UserPwd01" UserLevel="162" />
  <User UserID="UserID02" UserPwd="UserPwd02" UserLevel="165" />
  <User UserID="UserID03" UserPwd="UserPwd03" UserLevel="168" />
  <User UserID="UserID04" UserPwd="UserPwd04" UserLevel="171" />
  <User UserID="UserID05" UserPwd="UserPwd05" UserLevel="175" />
  <User UserID="UserID06" UserPwd="UserPwd06" UserLevel="178" />
  <User UserID="UserID07" UserPwd="UserPwd07" UserLevel="181" />
  <User UserID="UserID08" UserPwd="UserPwd08" UserLevel="184" />
  <User UserID="UserID09" UserPwd="UserPwd09" UserLevel="188" />
  </UserInfo>

七、產生
代碼如下:

CString strID = _T("");    
      CString strPwd = _T("");    
      CMarkup xml;    
      xml.SetDoc("xml version=\"1.0\" encoding=\"UTF-8\"?>");    
      xml.AddElem("UserInfo");    
      xml.IntoElem();    
      for (int i=0; i<10; i++)    
      {    
              //產生一個隨機數,作為權限    
              srand(UINT(time(NULL)+i));    
              int nLevel = rand()%256;    
              strID.Format("UserID%02d",i);    
              strPwd.Format("UserPwd%02d",i);    
              xml.AddElem("User");    
              xml.AddAttrib("UserID",strID);    
              xml.AddAttrib("UserPwd",strPwd);    
              xml.AddAttrib("UserLevel",nLevel);    
      }    
      xml.OutOfElem();    
      xml.Save("UserInfo1.xml");   
CString strID = _T("");
      CString strPwd = _T("");
      CMarkup xml;
      xml.SetDoc("xml version=\"1.0\" encoding=\"UTF-8\"?>");
      xml.AddElem("UserInfo");
      xml.IntoElem();
      for (int i=0; i<10; i++)
      {
              //產生一個隨機數,作為權限
              srand(UINT(time(NULL)+i));
              int nLevel = rand()%256;
              strID.Format("UserID%02d",i);
              strPwd.Format("UserPwd%02d",i);
              xml.AddElem("User");
              xml.AddAttrib("UserID",strID);
              xml.AddAttrib("UserPwd",strPwd);
              xml.AddAttrib("UserLevel",nLevel);
      }
      xml.OutOfElem();
      xml.Save("UserInfo1.xml");

八、查找
代碼如下:

CMarkup xml;    
BOOL bLoad = false;    
BOOL bFind = false;    
bLoad = xml.Load("UserInfo1.xml");    
      if (bLoad)    
      {    
              CString strID;    
              CString strPwd;    
              xml.ResetMainPos();    
              xml.FindElem();    //UserInfo    
              while (xml.FindChildElem("User"))    
              {    
                    strID = xml.GetChildAttrib("UserID");    
                    strPwd = xml.GetChildAttrib("UserPwd");    
                    TRACE("\n----id:%s,pwd:%s-------\n",strID,strPwd);    
                    if (strID=="UserID00"&&strPwd=="UserPwd00")    
                    {    
                            bFind = true;    
                            break;    
                    }    
              }    
      }    
      if (bFind)    
      {    
              TRACE("\n---- find------\n");    
      }    
      else    
      {    
              TRACE("\n----no find------\n");    
      }   
CMarkup xml;
BOOL bLoad = false;
BOOL bFind = false;
bLoad = xml.Load("UserInfo1.xml");
      if (bLoad)
      {
              CString strID;
              CString strPwd;
              xml.ResetMainPos();
              xml.FindElem();    //UserInfo
              while (xml.FindChildElem("User"))
              {
                    strID = xml.GetChildAttrib("UserID");
                    strPwd = xml.GetChildAttrib("UserPwd");
                    TRACE("\n----id:%s,pwd:%s-------\n",strID,strPwd);
                    if (strID=="UserID00"&&strPwd=="UserPwd00")
                    {
                            bFind = true;
                            break;
                    }
              }
      }
      if (bFind)
      {
              TRACE("\n---- find------\n");
      }
      else
      {
              TRACE("\n----no find------\n");
      }

九、修改
代碼如下:

CMarkup xml;    
      BOOL bLoad = false;    
      BOOL bFind = false;    
      bLoad = xml.Load("UserInfo1.xml");    
      if (bLoad)    
      {    
              CString strID;    
              CString strPwd;    
              xml.ResetMainPos();    
              xml.FindElem();    //UserInfo    
              while (xml.FindChildElem("User"))    
              {    
                    strID = xml.GetChildAttrib("UserID");    
                    strPwd = xml.GetChildAttrib("UserPwd");    
                    TRACE("\n----id:%s,pwd:%s-------\n",strID,strPwd);    
                    if (strID=="UserID00"&&strPwd=="UserPwd00")    
                    {    
                            bFind = true;    
                            xml.SetChildAttrib("UserID",strID+CString("Modify"));    
                            xml.Save("UserInfo1.xml");    
                            break;    
                    }    
              }    
      }    
      if (bFind)    
      {    
              TRACE("\n---- find------\n");    
      }    
      else    
      {    
              TRACE("\n----no find------\n");    
      }   

CMarkup字符集問題
CMarkup支持Unicode,_MBCS宏定義。VS默認設置為MBCS宏定義,未設置該宏時為UTF-8編碼。
有 MBCS宏定義,且 打開的文件是UTF-8編碼:
CMarkup在Load時將UTF-8轉換ANSI編碼格式,所以要在讀取的地方設置
#include <locale.h>
setlocale(LC_ALL, "");
沒有MBCS宏定義,且打開的文件是UTF-8編碼:
系統默認將文件按UTF-8格式讀入系統中,所以顯示的時候要手動將數據轉換為ANSI格式的進行顯示
#include <locale.h>
setlocale(LC_ALL, "");
strVal = CMarkup::UTF8ToA(strVal);
PS:通過strcpy等拷貝函數可以修正。
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved