XML解析类CMarkup的使用方法

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

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


1. 生成UserInfo.xml

 

[cpp] view plain copy
  1. CMarkup xml;       
  2. xml.SetDoc("xml version=/"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. CMarkup xml;   
  10. xml.SetDoc("xml version=/"1.0/" encoding=/"UTF-8/"?>/r/n");   
  11. xml.AddElem("UserInfo");   
  12. xml.IntoElem();   
  13. xml.AddElem("UserID","luo");   
  14. xml.AddElem("UserID","lin");   
  15. xml.OutOfElem();   
  16. xml.Save("UserInfo.xml");  


2. 浏览特定元素

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


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

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


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

3. 修改

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

[cpp] view plain copy
  1. BOOL bLoadXml = false;       
  2. CMarkup xml;       
  3. bLoadXml = xml.Load("UserInfo.xml");       
  4.       if (bLoadXml)       
  5.       {       
  6.               CString strUserID = _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. //--------把UserID为”luo”改为”flypigluo”-----------   
  22. BOOL bLoadXml = false;   
  23. CMarkup xml;   
  24. bLoadXml = xml.Load("UserInfo.xml");   
  25.       if (bLoadXml)   
  26.       {   
  27.               CString strUserID = _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)

//加在最后面

[cpp] view plain copy
  1. BOOL bLoadXml = false;       
  2.       CMarkup xml;       
  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. //--------原来为------       
  14. xml version="1.0" encoding="UTF-8" ?>       
  15. <UserInfo>       
  16.   <UserID>luo</UserID>       
  17.   <UserID>lin</UserID>       
  18.   </UserInfo>       
  19. //---------添加后为-------------       
  20. xml version="1.0" encoding="UTF-8" ?>       
  21. <UserInfo>       
  22.   <UserID>luo</UserID>       
  23.   <UserID>lin</UserID>       
  24.   <UserID>luoluo</UserID>       
  25.   </UserInfo>  


4.2  加在最前面(使用InsertElem)

[cpp] view plain copy
  1. BOOL bLoadXml = false;       
  2. CMarkup xml;       
  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. //----原来为—       
  14. xml version="1.0" encoding="UTF-8" ?>       
  15. <UserInfo>       
  16.   <UserID>luo</UserID>       
  17.   <UserID>lin</UserID>       
  18.   </UserInfo>       
  19. //------修改后-----       
  20. xml version="1.0" encoding="UTF-8" ?>       
  21. <UserInfo>       
  22.   <UserID>AddUserIDHead</UserID>       
  23.   <UserID>luo</UserID>       
  24.   <UserID>lin</UserID>       
  25.   </UserInfo>      
  26. BOOL bLoadXml = false;   
  27. CMarkup xml;   
  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. //----原来为—   
  39. xml version="1.0" encoding="UTF-8" ?>   
  40. <UserInfo>   
  41.   <UserID>luo</UserID>   
  42.   <UserID>lin</UserID>   
  43.   </UserInfo>   
  44. //------修改后-----   
  45. xml version="1.0" encoding="UTF-8" ?>   
  46. <UserInfo>   
  47.   <UserID>AddUserIDHead</UserID>   
  48.   <UserID>luo</UserID>   
  49.   <UserID>lin</UserID>   
  50.   </UserInfo>  


5. 删除

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


 

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

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


 

7. 产生

[cpp] view plain copy
  1. CString strID = _T("");       
  2.       CString strPwd = _T("");       
  3.       CMarkup xml;       
  4.       xml.SetDoc("xml version=/"1.0/" encoding=/"UTF-8/"?>");       
  5.       xml.AddElem("UserInfo");       
  6.       xml.IntoElem();       
  7.       for (int i=0; i<10; i++)       
  8.       {       
  9.               //产生一个随机数,作为权限       
  10.               srand(UINT(time(NULL)+i));       
  11.               int nLevel = 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. CString strID = _T("");   
  22.       CString strPwd = _T("");   
  23.       CMarkup xml;   
  24.       xml.SetDoc("xml version=/"1.0/" encoding=/"UTF-8/"?>");   
  25.       xml.AddElem("UserInfo");   
  26.       xml.IntoElem();   
  27.       for (int i=0; i<10; i++)   
  28.       {   
  29.               //产生一个随机数,作为权限   
  30.               srand(UINT(time(NULL)+i));   
  31.               int nLevel = rand()%256;   
  32.               strID.Format("UserID%02d",i);   
  33.               strPwd.Format("UserPwd%02d",i);   
  34.               xml.AddElem("User");   
  35.               xml.AddAttrib("UserID",strID);   
  36.               xml.AddAttrib("UserPwd",strPwd);   
  37.               xml.AddAttrib("UserLevel",nLevel);   
  38.       }   
  39.       xml.OutOfElem();   
  40.       xml.Save("UserInfo1.xml");  


 

8. 查找

[cpp] view plain copy
  1. CMarkup xml;       
  2. BOOL bLoad = false;       
  3. BOOL bFind = false;       
  4. bLoad = xml.Load("UserInfo1.xml");       
  5.       if (bLoad)       
  6.       {       
  7.               CString strID;       
  8.               CString strPwd;       
  9.               xml.ResetMainPos();       
  10.               xml.FindElem();    //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----no find------/n");       
  30.       }      
  31. CMarkup xml;   
  32. BOOL bLoad = false;   
  33. BOOL bFind = false;   
  34. bLoad = xml.Load("UserInfo1.xml");   
  35.       if (bLoad)   
  36.       {   
  37.               CString strID;   
  38.               CString strPwd;   
  39.               xml.ResetMainPos();   
  40.               xml.FindElem();    //UserInfo   
  41.               while (xml.FindChildElem("User"))   
  42.               {   
  43.                     strID = xml.GetChildAttrib("UserID");   
  44.                     strPwd = xml.GetChildAttrib("UserPwd");   
  45.                     TRACE("/n----id:%s,pwd:%s-------/n",strID,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----no find------/n");   
  60.       }  


 

9. 修改

[cpp] view plain copy
  1. CMarkup xml;       
  2.       BOOL bLoad = false;       
  3.       BOOL bFind = false;       
  4.       bLoad = xml.Load("UserInfo1.xml");       
  5.       if (bLoad)       
  6.       {       
  7.               CString strID;       
  8.               CString strPwd;       
  9.               xml.ResetMainPos();       
  10.               xml.FindElem();    //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.                             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----no find------/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] view plain copy
  1. #include <locale.h>  
  2. setlocale(LC_ALL, "");  


 

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

 

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


 

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

你可能感兴趣的:(XML解析类CMarkup的使用方法)