zoukankan      html  css  js  c++  java
  • vc中操作Xml使用CMarkup类

    DotNet中使用xml相对比较轻松,在vc中操作xml,有些繁琐。最近在网络上得知CMarkup比较好用,于是试了下,还好,就把部分例子写出来。以后有时间的话,再写清楚点。


    查看源代码拷贝至剪贴板打印代码
    //----------UserInfo.xml--------------





    luo

    lin


    //----------UserInfo.xml--------------





    luo

    lin



    1. 生成UserInfo.xml
    查看源代码拷贝至剪贴板打印代码
    CMarkup xml;
    xml.SetDoc(""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(""r"n");
    xml.AddElem("UserInfo");
    xml.IntoElem();
    xml.AddElem("UserID","luo");
    xml.AddElem("UserID","lin");
    xml.OutOfElem();
    xml.Save("UserInfo.xml");

    2. 浏览特定元素
    查看源代码拷贝至剪贴板打印代码
    //----查看所有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―

    3. 修改
    查看源代码拷贝至剪贴板打印代码
    //--------把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;
    }
    }
    }

    4. 添加
    4.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");
    }
    //--------原来为------


    luo
    lin

    //---------添加后为-------------


    luo
    lin
    luoluo

    //加在最后面
    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");
    }
    //--------原来为------


    luo
    lin

    //---------添加后为-------------


    luo
    lin
    luoluo


    4.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");
    }
    //----原来为―


    luo
    lin

    //------修改后-----


    AddUserIDHead
    luo
    lin

    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");
    }
    //----原来为―


    luo
    lin

    //------修改后-----


    AddUserIDHead
    luo
    lin


    5. 删除
    查看源代码拷贝至剪贴板打印代码
    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;
    }
    }
    }

    6. 查找
    见第二点浏览。类似上面的例子里面没有属性这些东西,下面的一个例子是有属性的

    查看源代码拷贝至剪贴板打印代码
    //UserInfo1.xml













    //UserInfo1.xml














    7. 产生
    查看源代码拷贝至剪贴板打印代码
    CString strID = _T("");
    CString strPwd = _T("");
    CMarkup xml;
    xml.SetDoc("");
    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.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");

    8. 查找
    查看源代码拷贝至剪贴板打印代码
    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");
    }

    9. 修改
    查看源代码拷贝至剪贴板打印代码
    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");
    }
  • 相关阅读:
    iframe自适应高度的多种方法
    jquery 限制上传文件的类型和大小
    20200303 pandas
    20200306 Linux基础
    20200305 VMware虚拟机安装及centOS
    20200310 CMDB基础设计
    20200407 算法与数据结构
    20200403 MongoDB操作以及pyMongo
    20200402 MongoDB安装及简介
    20200401 docker部署与mysql主从搭建django读写分离
  • 原文地址:https://www.cnblogs.com/whitetiger/p/1088389.html
Copyright © 2011-2022 走看看