XML操作一
//----------UserInfo.xml--------------
<?xmlversion="1.0"encoding="UTF-8"?>
<UserInfo>
<UserID>luo</UserID>
<UserID>lin</UserID>
</UserInfo>
1。生成UserInfo.xml
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");
2。浏览特定元素
//----查看所有UserID----
CMarkupxml;
xml.Load("UserInfo.xml");
BOOLbFind=true;
xml.ResetMainPos();
while(xml.FindChildElem("UserID");)
{
//此时接点还是父接点
CStringstrTagName=_T("");
CStringstrData=_T("");
strTagName=xml.GetChildTagName();
strData=xml.GetChildData();
TRACE("\n---tagName:%s,Data:%s--\n",strTagName,strData);
}
/********************这样也可以******************
CMarkupxml;
xml.Load("UserInfo.xml");
BOOLbFind=true;
xml.ResetMainPos();
while(xml.FindChildElem("UserID");)
{
xml.IntoElem();
CStringstrTagName=_T("");
CStringstrData=_T("");
strTagName=xml.GetTagName();
strData=xml.GetData();
TRACE("\n---tagName:%s,strData);
xml.OutOfElem();
}*/
/**************这样也可以**********************
CMarkupxml;
xml.Load("UserInfo.xml");
BOOLbFind=true;
xml.ResetMainPos();
xml.FindElem();//UserInfo
xml.IntoElem();
while(xml.FindElem("UserID");)
{
CStringstrTagName=_T("");
CStringstrData=_T("");
strTagName=xml.GetTagName();
strData=xml.GetData();
TRACE("\n---tagName:%s,strData);
}
*/
//-------------结果----------
---tagName:UserID,Data:luo--
---tagName:UserID,Data:lin—
3。修改
//--------把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)
//加在最后面
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");
}
//--------原来为------
<?xmlversion="1.0"encoding="UTF-8"?>
<UserInfo>
<UserID>luo</UserID>
<UserID>lin</UserID>
</UserInfo>
//---------添加后为-------------
<?xmlversion="1.0"encoding="UTF-8"?>
<UserInfo>
<UserID>luo</UserID>
<UserID>lin</UserID>
<UserID>luoluo</UserID>
</UserInfo>
4。2加在最前面(使用InsertElem)
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");
}
//----原来为—
<?xmlversion="1.0"encoding="UTF-8"?>
<UserInfo>
<UserID>luo</UserID>
<UserID>lin</UserID>
</UserInfo>
//------修改后-----
<?xmlversion="1.0"encoding="UTF-8"?>
<UserInfo>
<UserID>AddUserIDHead</UserID>
<UserID>luo</UserID>
<UserID>lin</UserID>
</UserInfo>
5。删除
CMarkupxml;
xml.Load("UserInfo.xml");
BOOLbFind=true;
xml.ResetMainPos();
while(bFind)
{
bFind=xml.FindChildElem("UserID");
if(bFind)
{//此时接点还是父接点
CStringstrData=_T("");
strData=xml.GetChildData();
if(strData=="luo")
{
xml.RemoveChildElem();
xml.Save("UserInfo.xml");
break;
}
}
}
6。查找
见第二点浏览。类似
上面的例子里面没有属性这些东西,下面的一个例子是有属性的
//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。产生
CStringstrID=_T("");
CStringstrPwd=_T("");
CMarkupxml;
xml.SetDoc("<?xmlversion=\"1.0\"encoding=\"UTF-8\"?>");
xml.AddElem("UserInfo");
xml.IntoElem();
for(inti=0;i<10;i++)
{//产生一个随机数,作为权限
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");
8。查找
CMarkupxml;
BOOLbLoad=false;
BOOLbFind=false;
bLoad=xml.Load("UserInfo1.xml");
if(bLoad)
{
CStringstrID;
CStringstrPwd;
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----nofind------\n");
}
9。修改
CMarkupxml;
BOOLbLoad=false;
BOOLbFind=false;
bLoad=xml.Load("UserInfo1.xml");
if(bLoad)
{
CStringstrID;
CStringstrPwd;
xml.ResetMainPos();
xml.FindElem();//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");
}
XML操作二
(一)先讲一下XML中的物殊字符,手动填写时注意一下。
字符字符实体
&&或&''或'>>或><<或&<""或"
(二)创建一个XML文档。
CMarkupxml;xml.AddElem("ORDER");xml.AddChildElem("ITEM");xml.IntoElem();xml.AddChildElem("SN","132487A-J");xml.AddChildElem("NAME","crankcasing");xml.AddChildElem("QTY","1");xml.Save("c:\\UserInfo.xml");
效果如下:
<ORDER><ITEM><SN>132487A-J</SN><NAME>crankcasing</NAME><QTY>1</QTY></ITEM></ORDER>
三浏览特定元素
CMarkupxml;
xml.Load("UserInfo.xml");while(xml.FindChildElem("ITEM"))
{
xml.IntoElem();
xml.FindChildElem("SN");
CStringcsSN=xml.GetChildData();
xml.FindChildElem("QTY");
intnQty=atoi(xml.GetChildData());
xml.OutOfElem();
}
添加在最后面,使用的是AddElem;添加在最前面,使用InsertElem。
CMarkupxml;
xml.Load("c:\\xml.AddElem("ORDER");
xml.IntoElem();//进入ORDER
xml.AddElem("ITEM");
//进入ITEM
xml.AddElem("SN","4238764-A");//添加元素
xml.AddElem("NAME",75)">bearing"xml.AddElem("QTY",75)">15"xml.OutOfElem();//退出ITEM
xml.AddElem("SHIPMENT");
//进入SHIPMENT
xml.AddElem("POC");//添加元素
xml.SetAttrib("type",75)">non-emergency");//添加属性
//进入POC
JohnSmith");//添加元素
xml.AddElem("TEL","555-1234");//添加元素
</QTY></ITEM><ITEM><SN>4238764-A</SN><NAME>bearing</NAME><QTY>15</QTY></ITEM><SHIPMENT><POCtype="non-emergency"><NAME>JohnSmith</NAME><TEL>555-1234</TEL></POC></SHIPMENT></ORDER>
如将POC中的属性type改成:change;
元素TEL改成:123456789
if(xml.Load("UserInfo.xml")){CStringstrUserID=_T("");xml.ResetMainPos();if(xml.FindChildElem("SHIPMENT"))if(xml.FindChildElem("POC"))CStringstr_type=xml.GetAttrib("type");MessageBox(str_type);strUserID=xml.GetData();if(xml.FindChildElem("TEL"))xml.SetData("123456789");xml.Save("UserInfo.xml");return;}六)删除元素:
删除SN=132487A-J的项目。
if(xml.FindChildElem("ITEM"))CStringstr_sn;xml.FindChildElem("SN");str_sn=xml.GetChildData();if(str_sn=="132487A-J")xml.RemoveElem();}
原文链接:https://www.f2er.com/xml/298480.html