VC解析XML文件的工具有很多,CMarkup, tinyXML,还有IBM的,MS的等等。
据说tinyXML很好,可能字符集问题,我编译不了。
所以就用CMarkup来解析,使用过后,觉得非常不错,使用起来很方便。
CMarkup下载地址:http://www.firstobject.com/
发现网上有方法很法,我就摘下来了
- //----------UserInfo.xml--------------
- xmlversion="1.0"encoding="UTF-8"?>
- <UserInfo>
- <UserID>luo</UserID>
- <UserID>lin</UserID>
- </UserInfo>
- //----------UserInfo.xml--------------
- xmlversion="1.0"encoding="UTF-8"?>
- <UserInfo>
- <UserID>luo</UserID>
- <UserID>lin</UserID>
- </UserInfo>
- CMarkupxml;
- xml.SetDoc("xmlversion=/"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");
- CMarkupxml;
- xml.SetDoc("xmlversion=/"1.0/"encoding=/"UTF-8/"?>/r/n");
- xml.AddElem("UserInfo");
- xml.IntoElem();
- xml.AddElem("UserID","lin");
- xml.OutOfElem();
- xml.Save("UserInfo.xml");
2. 浏览特定元素@H_301_10@
- CMarkupxml;
- xml.Load("UserInfo.xml");
- BOOLbFind=true;
- xml.ResetMainPos();
- while(xml.FindChildElem("UserID");)
- {
- @H_503_404@//此时接点还是父接点
- CStringstrTagName=_T("");
- CStringstrData=_T("");
- strTagName=xml.GetChildTagName();
- strData=xml.GetChildData();
- TRACE("/n---tagName:%s,Data:%s--/n",strTagName,strData);
- }
- @H_503_404@/********************这样也可以******************
- @H_503_404@CMarkupxml;
- @H_503_404@xml.Load("UserInfo.xml");
- @H_503_404@BOOLbFind=true;
- @H_503_404@xml.ResetMainPos();
- @H_503_404@
- @H_503_404@while(xml.FindChildElem("UserID");)
- @H_503_404@{
- @H_503_404@xml.IntoElem();
- @H_503_404@CStringstrTagName=_T("");
- @H_503_404@CStringstrData=_T("");
- @H_503_404@strTagName=xml.GetTagName();
- @H_503_404@strData=xml.GetData();
- @H_503_404@TRACE("/n---tagName:%s,Data:%s--/n",strData);
- @H_503_404@xml.OutOfElem();
- @H_503_404@}*/
- @H_503_404@/**************这样也可以**********************
- @H_503_404@CMarkupxml;
- @H_503_404@xml.Load("UserInfo.xml");
- @H_503_404@BOOLbFind=true;
- @H_503_404@xml.ResetMainPos();
- @H_503_404@xml.FindElem();//UserInfo
- @H_503_404@xml.IntoElem();
- @H_503_404@while(xml.FindElem("UserID");)
- @H_503_404@{
- @H_503_404@CStringstrTagName=_T("");
- @H_503_404@CStringstrData=_T("");
- @H_503_404@strTagName=xml.GetTagName();
- @H_503_404@strData=xml.GetData();
- @H_503_404@TRACE("/n---tagName:%s,strData);
- @H_503_404@}
- @H_503_404@*/
//-------------结果----------
---tagName:UserID,Data:luo--
---tagName:UserID,Data:lin—
- CMarkupxml;
- xml.Load("UserInfo.xml");
- BOOLbFind=true;
- xml.ResetMainPos();
- while(xml.FindChildElem("UserID");)
- {
- @H_503_404@//此时接点还是父接点
- CStringstrTagName=_T("");
- CStringstrData=_T("");
- strTagName=xml.GetChildTagName();
- strData=xml.GetChildData();
- TRACE("/n---tagName:%s,Data:lin—
3. 修改
@H_301_10@//--------把UserID为”luo”改为”flypigluo”------------ BOOLbLoadXml=false;
- CMarkupxml;
- bLoadXml=xml.Load("UserInfo.xml");
- if(bLoadXml)
- {
- CStringstrUserID=_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;
- }
- }
- }
- @H_503_404@//--------把UserID为”luo”改为”flypigluo”-----------
- BOOLbLoadXml=false;
- CMarkupxml;
- bLoadXml=xml.Load("UserInfo.xml");
- if(bLoadXml)
- {
- CStringstrUserID=_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)@H_301_10@
//加在最后面- BOOLbLoadXml=false;
- CMarkupxml;
- bLoadXml=xml.Load("UserInfo.xml");
- if(bLoadXml)
- {
- xml.ResetMainPos();
- xml.FindElem();
- xml.IntoElem();
- xml.AddElem("UserID","luoluo");
- xml.OutOfElem();
- xml.Save("UserInfo.xml");
- }
- @H_503_404@//--------原来为------
- xmlversion="1.0"encoding="UTF-8"?>
- <UserInfo>
- <UserID>luo</UserID>
- <UserID>lin</UserID>
- </UserInfo>
- @H_503_404@//---------添加后为-------------
- xmlversion="1.0"encoding="UTF-8"?>
- <UserInfo>
- <UserID>luo</UserID>
- <UserID>lin</UserID>
- <UserID>luoluo</UserID>
- </UserInfo>
4.2 加在最前面(使用InsertElem)@H_301_10@- BOOLbLoadXml=false;
- CMarkupxml;
- bLoadXml=xml.Load("UserInfo.xml");
- if(bLoadXml)
- {
- xml.ResetMainPos();
- xml.FindElem();
- xml.IntoElem();
- xml.InsertElem("UserID","AddUserIDHead");
- xml.OutOfElem();
- xml.Save("UserInfo.xml");
- }
- @H_503_404@//----原来为—
- xmlversion="1.0"encoding="UTF-8"?>
- <UserInfo>
- <UserID>luo</UserID>
- <UserID>lin</UserID>
- </UserInfo>
- @H_503_404@//------修改后-----
- xmlversion="1.0"encoding="UTF-8"?>
- <UserInfo>
- <UserID>AddUserIDHead</UserID>
- <UserID>luo</UserID>
- <UserID>lin</UserID>
- </UserInfo>
- BOOLbLoadXml=false;
- CMarkupxml;
- bLoadXml=xml.Load("UserInfo.xml");
- if(bLoadXml)
- {
- xml.ResetMainPos();
- xml.FindElem();
- xml.IntoElem();
- xml.InsertElem("UserID","AddUserIDHead");
- xml.OutOfElem();
- xml.Save("UserInfo.xml");
- }
- @H_503_404@//----原来为—
- xmlversion="1.0"encoding="UTF-8"?>
- <UserInfo>
- <UserID>luo</UserID>
- <UserID>lin</UserID>
- </UserInfo>
- @H_503_404@//------修改后-----
- xmlversion="1.0"encoding="UTF-8"?>
- <UserInfo>
- <UserID>AddUserIDHead</UserID>
- <UserID>luo</UserID>
- <UserID>lin</UserID>
- </UserInfo>
- CMarkupxml;
- xml.Load("UserInfo.xml");
- BOOLbFind=true;
- xml.ResetMainPos();
- while(bFind)
- {
- bFind=xml.FindChildElem("UserID");
- if(bFind)
- {
- @H_503_404@//此时接点还是父接点
- CStringstrData=_T("");
- strData=xml.GetChildData();
- if(strData=="luo")
- {
- xml.RemoveChildElem();
- xml.Save("UserInfo.xml");
- break;
- }
- }
- }
- CMarkupxml;
- xml.Load("UserInfo.xml");
- BOOLbFind=true;
- xml.ResetMainPos();
- while(bFind)
- {
- bFind=xml.FindChildElem("UserID");
- if(bFind)
- {
- @H_503_404@//此时接点还是父接点
- CStringstrData=_T("");
- strData=xml.GetChildData();
- if(strData=="luo")
- {
- xml.RemoveChildElem();
- xml.Save("UserInfo.xml");
- break;
- }
- }
- }
6. 查找
@H_301_10@见第二点浏览。类似上面的例子里面没有属性这些东西,下面的一个例子是有属性的- @H_503_404@//UserInfo1.xml
- xmlversion="1.0"encoding="UTF-8"?>
- <UserInfo>
- <UserUserID="UserID00"UserPwd="UserPwd00"UserLevel="158"/>
- <UserUserID="UserID01"UserPwd="UserPwd01"UserLevel="162"/>
- <UserUserID="UserID02"UserPwd="UserPwd02"UserLevel="165"/>
- <UserUserID="UserID03"UserPwd="UserPwd03"UserLevel="168"/>
- <UserUserID="UserID04"UserPwd="UserPwd04"UserLevel="171"/>
- <UserUserID="UserID05"UserPwd="UserPwd05"UserLevel="175"/>
- <UserUserID="UserID06"UserPwd="UserPwd06"UserLevel="178"/>
- <UserUserID="UserID07"UserPwd="UserPwd07"UserLevel="181"/>
- <UserUserID="UserID08"UserPwd="UserPwd08"UserLevel="184"/>
- <UserUserID="UserID09"UserPwd="UserPwd09"UserLevel="188"/>
- </UserInfo>
- @H_503_404@//UserInfo1.xml
- xmlversion="1.0"encoding="UTF-8"?>
- <UserInfo>
- <UserUserID="UserID00"UserPwd="UserPwd00"UserLevel="158"/>
- <UserUserID="UserID01"UserPwd="UserPwd01"UserLevel="162"/>
- <UserUserID="UserID02"UserPwd="UserPwd02"UserLevel="165"/>
- <UserUserID="UserID03"UserPwd="UserPwd03"UserLevel="168"/>
- <UserUserID="UserID04"UserPwd="UserPwd04"UserLevel="171"/>
- <UserUserID="UserID05"UserPwd="UserPwd05"UserLevel="175"/>
- <UserUserID="UserID06"UserPwd="UserPwd06"UserLevel="178"/>
- <UserUserID="UserID07"UserPwd="UserPwd07"UserLevel="181"/>
- <UserUserID="UserID08"UserPwd="UserPwd08"UserLevel="184"/>
- <UserUserID="UserID09"UserPwd="UserPwd09"UserLevel="188"/>
- </UserInfo>
7. 产生@H_301_10@
- CStringstrID=_T("");
- CStringstrPwd=_T("");
- CMarkupxml;
- xml.SetDoc("xmlversion=/"1.0/"encoding=/"UTF-8/"?>");
- xml.AddElem("UserInfo");
- xml.IntoElem();
- for(inti=0;i<10;i++)
- {
- @H_503_404@//产生一个随机数,作为权限
- srand(UINT(time(NULL)+i));
- intnLevel=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");
- CStringstrID=_T("");
- CStringstrPwd=_T("");
- CMarkupxml;
- xml.SetDoc("xmlversion=/"1.0/"encoding=/"UTF-8/"?>");
- xml.AddElem("UserInfo");
- xml.IntoElem();
- for(inti=0;i<10;i++)
- {
- @H_503_404@//产生一个随机数,作为权限
- srand(UINT(time(NULL)+i));
- intnLevel=rand()%256;
- strID.Format("UserID%02d",nLevel);
- }
- xml.OutOfElem();
- xml.Save("UserInfo1.xml");
8. 查找@H_301_10@
- CMarkupxml;
- BOOLbLoad=false;
- BOOLbFind=false;
- bLoad=xml.Load("UserInfo1.xml");
- if(bLoad)
- {
- CStringstrID;
- CStringstrPwd;
- xml.ResetMainPos();
- xml.FindElem();@H_503_404@//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----nofind------/n");
- }
- CMarkupxml;
- BOOLbLoad=false;
- BOOLbFind=false;
- bLoad=xml.Load("UserInfo1.xml");
- if(bLoad)
- {
- CStringstrID;
- CStringstrPwd;
- xml.ResetMainPos();
- xml.FindElem();@H_503_404@//UserInfo
- while(xml.FindChildElem("User"))
- {
- strID=xml.GetChildAttrib("UserID");
- strPwd=xml.GetChildAttrib("UserPwd");
- TRACE("/n----id:%s,strPwd);
- if(strID=="UserID00"&&strPwd=="UserPwd00")
- {
- bFind=true;
- break;
- }
- }
- }
- if(bFind)
- {
- TRACE("/n----find------/n");
- }
- else
- {
- TRACE("/n----nofind------/n");
- }
- CMarkupxml;
- BOOLbLoad=false;
- BOOLbFind=false;
- bLoad=xml.Load("UserInfo1.xml");
- if(bLoad)
- {
- CStringstrID;
- CStringstrPwd;
- xml.ResetMainPos();
- xml.FindElem();@H_503_404@//UserInfo
- while(xml.FindChildElem("User"))
- {
- strID=xml.GetChildAttrib("UserID");
- strPwd=xml.GetChildAttrib("UserPwd");
- TRACE("/n----id:%s,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----nofind------/n");
- }
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编码格式,所以要在读取的地方设置- #include<locale.h>
- setlocale(LC_ALL,"");
没有MBCS宏定义,且打开的文件是UTF-8编码:
系统默认将文件按UTF-8格式读入系统中,所以显示的时候要手动将数据转换为ANSI格式的进行显示- #include<locale.h>
- setlocale(LC_ALL,"");
- strVal=CMarkup::UTF8ToA(strVal);
PS:通过strcpy等拷贝函数可以修正
原文链接:https://www.f2er.com/xml/296629.html