XML解析类CMarkup的使用方法

前端之家收集整理的这篇文章主要介绍了XML解析类CMarkup的使用方法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

VC解析XML文件的工具有很多,CMarkup, tinyXML,还有IBM的,MS的等等。
据说tinyXML很好,可能字符集问题,我编译不了。
所以就用CMarkup来解析,使用过后,觉得非常不错,使用起来很方便。
CMarkup下载地址:http://www.firstobject.com/
发现网上有方法很法,我就摘下来了

[html]@H_301_10@ view plain copy
  1. //----------UserInfo.xml--------------
  2. xmlversion="1.0"encoding="UTF-8"?>
  3. <UserInfo>
  4. <UserID>luo</UserID>
  5. <UserID>lin</UserID>
  6. </UserInfo>
  7. //----------UserInfo.xml--------------
  8. xmlversion="1.0"encoding="UTF-8"?>
  9. <UserInfo>
  10. <UserID>luo</UserID>
  11. <UserID>lin</UserID>
  12. </UserInfo>


1. 生成UserInfo.xml@H_301_10@

[cpp]@H_301_10@ view plain copy
  1. CMarkupxml;
  2. xml.SetDoc("xmlversion=/"1.0/"encoding=/"UTF-8/"?>/r/n");
  3. xml.AddElem("UserInfo");
  4. xml.IntoElem();
  5. xml.AddElem("UserID","luo");
  6. xml.AddElem("UserID","lin");
  7. xml.OutOfElem();
  8. xml.Save("UserInfo.xml");
  9. CMarkupxml;
  10. xml.SetDoc("xmlversion=/"1.0/"encoding=/"UTF-8/"?>/r/n");
  11. xml.AddElem("UserInfo");
  12. xml.IntoElem();
  13. xml.AddElem("UserID","lin");
  14. xml.OutOfElem();
  15. xml.Save("UserInfo.xml");


2. 浏览特定元素@H_301_10@

[cpp]@H_301_10@ view plain copy
  1. CMarkupxml;
  2. xml.Load("UserInfo.xml");
  3. BOOLbFind=true;
  4. xml.ResetMainPos();
  5. while(xml.FindChildElem("UserID");)
  6. {
  7. @H_503_404@//此时接点还是父接点
  8. CStringstrTagName=_T("");
  9. CStringstrData=_T("");
  10. strTagName=xml.GetChildTagName();
  11. strData=xml.GetChildData();
  12. TRACE("/n---tagName:%s,Data:%s--/n",strTagName,strData);
  13. }
  14. @H_503_404@/********************这样也可以******************
  15. @H_503_404@CMarkupxml;
  16. @H_503_404@xml.Load("UserInfo.xml");
  17. @H_503_404@BOOLbFind=true;
  18. @H_503_404@xml.ResetMainPos();
  19. @H_503_404@
  20. @H_503_404@while(xml.FindChildElem("UserID");)
  21. @H_503_404@{
  22. @H_503_404@xml.IntoElem();
  23. @H_503_404@CStringstrTagName=_T("");
  24. @H_503_404@CStringstrData=_T("");
  25. @H_503_404@strTagName=xml.GetTagName();
  26. @H_503_404@strData=xml.GetData();
  27. @H_503_404@TRACE("/n---tagName:%s,Data:%s--/n",strData);
  28. @H_503_404@xml.OutOfElem();
  29. @H_503_404@}*/
  30. @H_503_404@/**************这样也可以**********************
  31. @H_503_404@CMarkupxml;
  32. @H_503_404@xml.Load("UserInfo.xml");
  33. @H_503_404@BOOLbFind=true;
  34. @H_503_404@xml.ResetMainPos();
  35. @H_503_404@xml.FindElem();//UserInfo
  36. @H_503_404@xml.IntoElem();
  37. @H_503_404@while(xml.FindElem("UserID");)
  38. @H_503_404@{
  39. @H_503_404@CStringstrTagName=_T("");
  40. @H_503_404@CStringstrData=_T("");
  41. @H_503_404@strTagName=xml.GetTagName();
  42. @H_503_404@strData=xml.GetData();
  43. @H_503_404@TRACE("/n---tagName:%s,strData);
  44. @H_503_404@}
  45. @H_503_404@*/


//-------------结果----------
---tagName:UserID,Data:luo--
---tagName:UserID,Data:lin—

[cpp]@H_301_10@ view plain copy
  1. CMarkupxml;
  2. xml.Load("UserInfo.xml");
  3. BOOLbFind=true;
  4. xml.ResetMainPos();
  5. while(xml.FindChildElem("UserID");)
  6. {
  7. @H_503_404@//此时接点还是父接点
  8. CStringstrTagName=_T("");
  9. CStringstrData=_T("");
  10. strTagName=xml.GetChildTagName();
  11. strData=xml.GetChildData();
  12. TRACE("/n---tagName:%s,Data:lin—

    3. 修改

    @H_301_10@//--------把UserID为”luo”改为”flypigluo”-----------

    [cpp]@H_301_10@ view plain copy
    1. BOOLbLoadXml=false;
    2. CMarkupxml;
    3. bLoadXml=xml.Load("UserInfo.xml");
    4. if(bLoadXml)
    5. {
    6. CStringstrUserID=_T("");
    7. xml.ResetMainPos();
    8. xml.FindElem();
    9. xml.IntoElem();
    10. while(xml.FindElem("UserID"))
    11. {
    12. strUserID=xml.GetData();
    13. if(strUserID=="luo")
    14. {
    15. xml.SetData(CString("flypig")+strUserID);
    16. xml.Save("UserInfo.xml");
    17. break;
    18. }
    19. }
    20. }
    21. @H_503_404@//--------把UserID为”luo”改为”flypigluo”-----------
    22. BOOLbLoadXml=false;
    23. CMarkupxml;
    24. bLoadXml=xml.Load("UserInfo.xml");
    25. if(bLoadXml)
    26. {
    27. CStringstrUserID=_T("");
    28. xml.ResetMainPos();
    29. xml.FindElem();
    30. xml.IntoElem();
    31. while(xml.FindElem("UserID"))
    32. {
    33. strUserID=xml.GetData();
    34. if(strUserID=="luo")
    35. {
    36. xml.SetData(CString("flypig")+strUserID);
    37. xml.Save("UserInfo.xml");
    38. break;
    39. }
    40. }
    41. }


    4. 添加

    4.1 添在最后面(使用的是AddElem)@H_301_10@
    //加在最后面

    [cpp]@H_301_10@ view plain copy
    1. BOOLbLoadXml=false;
    2. CMarkupxml;
    3. bLoadXml=xml.Load("UserInfo.xml");
    4. if(bLoadXml)
    5. {
    6. xml.ResetMainPos();
    7. xml.FindElem();
    8. xml.IntoElem();
    9. xml.AddElem("UserID","luoluo");
    10. xml.OutOfElem();
    11. xml.Save("UserInfo.xml");
    12. }
    13. @H_503_404@//--------原来为------
    14. xmlversion="1.0"encoding="UTF-8"?>
    15. <UserInfo>
    16. <UserID>luo</UserID>
    17. <UserID>lin</UserID>
    18. </UserInfo>
    19. @H_503_404@//---------添加后为-------------
    20. xmlversion="1.0"encoding="UTF-8"?>
    21. <UserInfo>
    22. <UserID>luo</UserID>
    23. <UserID>lin</UserID>
    24. <UserID>luoluo</UserID>
    25. </UserInfo>


    4.2 加在最前面(使用InsertElem)@H_301_10@

    [cpp]@H_301_10@ view plain copy
    1. BOOLbLoadXml=false;
    2. CMarkupxml;
    3. bLoadXml=xml.Load("UserInfo.xml");
    4. if(bLoadXml)
    5. {
    6. xml.ResetMainPos();
    7. xml.FindElem();
    8. xml.IntoElem();
    9. xml.InsertElem("UserID","AddUserIDHead");
    10. xml.OutOfElem();
    11. xml.Save("UserInfo.xml");
    12. }
    13. @H_503_404@//----原来为—
    14. xmlversion="1.0"encoding="UTF-8"?>
    15. <UserInfo>
    16. <UserID>luo</UserID>
    17. <UserID>lin</UserID>
    18. </UserInfo>
    19. @H_503_404@//------修改后-----
    20. xmlversion="1.0"encoding="UTF-8"?>
    21. <UserInfo>
    22. <UserID>AddUserIDHead</UserID>
    23. <UserID>luo</UserID>
    24. <UserID>lin</UserID>
    25. </UserInfo>
    26. BOOLbLoadXml=false;
    27. CMarkupxml;
    28. bLoadXml=xml.Load("UserInfo.xml");
    29. if(bLoadXml)
    30. {
    31. xml.ResetMainPos();
    32. xml.FindElem();
    33. xml.IntoElem();
    34. xml.InsertElem("UserID","AddUserIDHead");
    35. xml.OutOfElem();
    36. xml.Save("UserInfo.xml");
    37. }
    38. @H_503_404@//----原来为—
    39. xmlversion="1.0"encoding="UTF-8"?>
    40. <UserInfo>
    41. <UserID>luo</UserID>
    42. <UserID>lin</UserID>
    43. </UserInfo>
    44. @H_503_404@//------修改后-----
    45. xmlversion="1.0"encoding="UTF-8"?>
    46. <UserInfo>
    47. <UserID>AddUserIDHead</UserID>
    48. <UserID>luo</UserID>
    49. <UserID>lin</UserID>
    50. </UserInfo>


    5. 删除@H_301_10@

    [cpp]@H_301_10@ view plain copy
    1. CMarkupxml;
    2. xml.Load("UserInfo.xml");
    3. BOOLbFind=true;
    4. xml.ResetMainPos();
    5. while(bFind)
    6. {
    7. bFind=xml.FindChildElem("UserID");
    8. if(bFind)
    9. {
    10. @H_503_404@//此时接点还是父接点
    11. CStringstrData=_T("");
    12. strData=xml.GetChildData();
    13. if(strData=="luo")
    14. {
    15. xml.RemoveChildElem();
    16. xml.Save("UserInfo.xml");
    17. break;
    18. }
    19. }
    20. }
    21. CMarkupxml;
    22. xml.Load("UserInfo.xml");
    23. BOOLbFind=true;
    24. xml.ResetMainPos();
    25. while(bFind)
    26. {
    27. bFind=xml.FindChildElem("UserID");
    28. if(bFind)
    29. {
    30. @H_503_404@//此时接点还是父接点
    31. CStringstrData=_T("");
    32. strData=xml.GetChildData();
    33. if(strData=="luo")
    34. {
    35. xml.RemoveChildElem();
    36. xml.Save("UserInfo.xml");
    37. break;
    38. }
    39. }
    40. }


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

    [cpp]@H_301_10@ view plain copy
    1. @H_503_404@//UserInfo1.xml
    2. xmlversion="1.0"encoding="UTF-8"?>
    3. <UserInfo>
    4. <UserUserID="UserID00"UserPwd="UserPwd00"UserLevel="158"/>
    5. <UserUserID="UserID01"UserPwd="UserPwd01"UserLevel="162"/>
    6. <UserUserID="UserID02"UserPwd="UserPwd02"UserLevel="165"/>
    7. <UserUserID="UserID03"UserPwd="UserPwd03"UserLevel="168"/>
    8. <UserUserID="UserID04"UserPwd="UserPwd04"UserLevel="171"/>
    9. <UserUserID="UserID05"UserPwd="UserPwd05"UserLevel="175"/>
    10. <UserUserID="UserID06"UserPwd="UserPwd06"UserLevel="178"/>
    11. <UserUserID="UserID07"UserPwd="UserPwd07"UserLevel="181"/>
    12. <UserUserID="UserID08"UserPwd="UserPwd08"UserLevel="184"/>
    13. <UserUserID="UserID09"UserPwd="UserPwd09"UserLevel="188"/>
    14. </UserInfo>
    15. @H_503_404@//UserInfo1.xml
    16. xmlversion="1.0"encoding="UTF-8"?>
    17. <UserInfo>
    18. <UserUserID="UserID00"UserPwd="UserPwd00"UserLevel="158"/>
    19. <UserUserID="UserID01"UserPwd="UserPwd01"UserLevel="162"/>
    20. <UserUserID="UserID02"UserPwd="UserPwd02"UserLevel="165"/>
    21. <UserUserID="UserID03"UserPwd="UserPwd03"UserLevel="168"/>
    22. <UserUserID="UserID04"UserPwd="UserPwd04"UserLevel="171"/>
    23. <UserUserID="UserID05"UserPwd="UserPwd05"UserLevel="175"/>
    24. <UserUserID="UserID06"UserPwd="UserPwd06"UserLevel="178"/>
    25. <UserUserID="UserID07"UserPwd="UserPwd07"UserLevel="181"/>
    26. <UserUserID="UserID08"UserPwd="UserPwd08"UserLevel="184"/>
    27. <UserUserID="UserID09"UserPwd="UserPwd09"UserLevel="188"/>
    28. </UserInfo>


    7. 产生@H_301_10@

    [cpp]@H_301_10@ view plain copy
    1. CStringstrID=_T("");
    2. CStringstrPwd=_T("");
    3. CMarkupxml;
    4. xml.SetDoc("xmlversion=/"1.0/"encoding=/"UTF-8/"?>");
    5. xml.AddElem("UserInfo");
    6. xml.IntoElem();
    7. for(inti=0;i<10;i++)
    8. {
    9. @H_503_404@//产生一个随机数,作为权限
    10. srand(UINT(time(NULL)+i));
    11. intnLevel=rand()%256;
    12. strID.Format("UserID%02d",i);
    13. strPwd.Format("UserPwd%02d",i);
    14. xml.AddElem("User");
    15. xml.AddAttrib("UserID",strID);
    16. xml.AddAttrib("UserPwd",strPwd);
    17. xml.AddAttrib("UserLevel",nLevel);
    18. }
    19. xml.OutOfElem();
    20. xml.Save("UserInfo1.xml");
    21. CStringstrID=_T("");
    22. CStringstrPwd=_T("");
    23. CMarkupxml;
    24. xml.SetDoc("xmlversion=/"1.0/"encoding=/"UTF-8/"?>");
    25. xml.AddElem("UserInfo");
    26. xml.IntoElem();
    27. for(inti=0;i<10;i++)
    28. {
    29. @H_503_404@//产生一个随机数,作为权限
    30. srand(UINT(time(NULL)+i));
    31. intnLevel=rand()%256;
    32. strID.Format("UserID%02d",nLevel);
    33. }
    34. xml.OutOfElem();
    35. xml.Save("UserInfo1.xml");


    8. 查找@H_301_10@

    [cpp]@H_301_10@ view plain copy
    1. CMarkupxml;
    2. BOOLbLoad=false;
    3. BOOLbFind=false;
    4. bLoad=xml.Load("UserInfo1.xml");
    5. if(bLoad)
    6. {
    7. CStringstrID;
    8. CStringstrPwd;
    9. xml.ResetMainPos();
    10. xml.FindElem();@H_503_404@//UserInfo
    11. while(xml.FindChildElem("User"))
    12. {
    13. strID=xml.GetChildAttrib("UserID");
    14. strPwd=xml.GetChildAttrib("UserPwd");
    15. TRACE("/n----id:%s,pwd:%s-------/n",strID,strPwd);
    16. if(strID=="UserID00"&&strPwd=="UserPwd00")
    17. {
    18. bFind=true;
    19. break;
    20. }
    21. }
    22. }
    23. if(bFind)
    24. {
    25. TRACE("/n----find------/n");
    26. }
    27. else
    28. {
    29. TRACE("/n----nofind------/n");
    30. }
    31. CMarkupxml;
    32. BOOLbLoad=false;
    33. BOOLbFind=false;
    34. bLoad=xml.Load("UserInfo1.xml");
    35. if(bLoad)
    36. {
    37. CStringstrID;
    38. CStringstrPwd;
    39. xml.ResetMainPos();
    40. xml.FindElem();@H_503_404@//UserInfo
    41. while(xml.FindChildElem("User"))
    42. {
    43. strID=xml.GetChildAttrib("UserID");
    44. strPwd=xml.GetChildAttrib("UserPwd");
    45. TRACE("/n----id:%s,strPwd);
    46. if(strID=="UserID00"&&strPwd=="UserPwd00")
    47. {
    48. bFind=true;
    49. break;
    50. }
    51. }
    52. }
    53. if(bFind)
    54. {
    55. TRACE("/n----find------/n");
    56. }
    57. else
    58. {
    59. TRACE("/n----nofind------/n");
    60. }


    9. 修改@H_301_10@

    [cpp]@H_301_10@ view plain copy
    1. CMarkupxml;
    2. BOOLbLoad=false;
    3. BOOLbFind=false;
    4. bLoad=xml.Load("UserInfo1.xml");
    5. if(bLoad)
    6. {
    7. CStringstrID;
    8. CStringstrPwd;
    9. xml.ResetMainPos();
    10. xml.FindElem();@H_503_404@//UserInfo
    11. while(xml.FindChildElem("User"))
    12. {
    13. strID=xml.GetChildAttrib("UserID");
    14. strPwd=xml.GetChildAttrib("UserPwd");
    15. TRACE("/n----id:%s,strPwd);
    16. if(strID=="UserID00"&&strPwd=="UserPwd00")
    17. {
    18. bFind=true;
    19. xml.SetChildAttrib("UserID",strID+CString("Modify"));
    20. xml.Save("UserInfo1.xml");
    21. break;
    22. }
    23. }
    24. }
    25. if(bFind)
    26. {
    27. TRACE("/n----find------/n");
    28. }
    29. else
    30. {
    31. TRACE("/n----nofind------/n");
    32. }


    CMarkup字符集问题
    daubwu posted @ 2008年9月08日 00:54 in VC with tags XML CMarkup,38 阅读
    CMarkup支持Unicode,_MBCS宏定义。VS默认设置为MBCS宏定义,未设置该宏时为UTF-8编码。
    有 MBCS宏定义,且 打开的文件是UTF-8编码:
    CMarkup在Load时将UTF-8转换ANSI编码格式,所以要在读取的地方设置

    [cpp]@H_301_10@ view plain copy
    1. #include<locale.h>
    2. setlocale(LC_ALL,"");


    没有MBCS宏定义,且打开的文件是UTF-8编码:
    系统默认将文件按UTF-8格式读入系统中,所以显示的时候要手动将数据转换为ANSI格式的进行显示

    [cpp]@H_301_10@ view plain copy
    1. #include<locale.h>
    2. setlocale(LC_ALL,"");
    3. strVal=CMarkup::UTF8ToA(strVal);


    PS:通过strcpy等拷贝函数可以修正

    原文链接:https://www.f2er.com/xml/296629.html

猜你在找的XML相关文章