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

IW1F7S

IW1F7S

2016-02-19 08:59

下面图老师小编跟大家分享VC解析XML文件-CMarkup的使用详解,一起来学习下过程究竟如何进行吧!喜欢就赶紧收藏起来哦~
VC解析XML文件的工具有很多,CMarkup, tinyXML,还有IBM的,MS的等等。
据说tinyXML很好,可能字符集问题,我编译不了。
所以就用CMarkup来解析,使用过后,觉得非常不错,使用起来很方便。
CMarkup下载地址:http://www.firstobject.com/
发现网上有方法很法,我就摘下来了
代码如下:

//----------UserInfo.xml--------------   
xml version="1.0" encoding="UTF-8" ?    
UserInfo   
  UserIDluo/UserID    
  UserIDlin/UserID    
  /UserInfo  
//----------UserInfo.xml--------------
xml version="1.0" encoding="UTF-8" ?
UserInfo
UserIDluo/UserID
UserIDlin/UserID
/UserInfo

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

CMarkup xml;    
xml.SetDoc("xml version="1.0" encoding="UTF-8"?rn");    
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"?rn");
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    
  UserIDluo/UserID    
  UserIDlin/UserID    
  /UserInfo    
//---------添加后为-------------    
xml version="1.0" encoding="UTF-8" ?    
UserInfo    
  UserIDluo/UserID    
  UserIDlin/UserID    
  UserIDluoluo/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    
  UserIDluo/UserID    
  UserIDlin/UserID    
  /UserInfo    
//------修改后-----    
xml version="1.0" encoding="UTF-8" ?    
UserInfo    
  UserIDAddUserIDHead/UserID    
  UserIDluo/UserID    
  UserIDlin/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
  UserIDluo/UserID
  UserIDlin/UserID
  /UserInfo
//------修改后-----
xml version="1.0" encoding="UTF-8" ?
UserInfo
  UserIDAddUserIDHead/UserID
  UserIDluo/UserID
  UserIDlin/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; i10; 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; i10; 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等拷贝函数可以修正。
展开更多 50%)
分享

猜你喜欢

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

编程语言 网络编程
VC解析XML文件-CMarkup的使用详解

Android开发之XML文件解析的使用

编程语言 网络编程
Android开发之XML文件解析的使用

s8lol主宰符文怎么配

英雄联盟 网络游戏
s8lol主宰符文怎么配

VC中多文件工程的PCLint使用详解

编程语言 网络编程
VC中多文件工程的PCLint使用详解

Android中使用sax解析xml文件的方法

编程语言 网络编程
Android中使用sax解析xml文件的方法

lol偷钱流符文搭配推荐

英雄联盟 网络游戏
lol偷钱流符文搭配推荐

jQuery 解析xml文件

Web开发
jQuery 解析xml文件

Android中使用PULL方式解析XML文件深入介绍

编程语言 网络编程
Android中使用PULL方式解析XML文件深入介绍

lolAD刺客新符文搭配推荐

英雄联盟
lolAD刺客新符文搭配推荐

Javascript 各浏览器的 Javascript 效率对比

Javascript 各浏览器的 Javascript 效率对比

深入C++中API的问题详解

深入C++中API的问题详解
下拉加载更多内容 ↓