2012-02-11 14:46:19|?次阅读|上传:wustguangh【已有?条评论】发表评论
关键词:C/C++, XML, 数据库|来源:唯设编程网
.Net中使用xml相对比较轻松,在vc中操作xml,有些繁琐。但是使用CMarkup以后,VC对XML的操作变得和.NET一样甚至更加简单,本文通过实际的源码,介绍了CMarkup操作XML文件的基本方法。
》测试用XML文件1
//----------UserInfo.xml--------------
<?xml version="1.0" encoding="UTF-8" ?> <UserInfo> <UserID>luo</UserID> <UserID>lin</UserID> </UserInfo>
》生成UserInfo.xml
CMarkup xml; xml.SetDoc("<?xml version="1.0" encoding="UTF-8"?> "); xml.AddElem("UserInfo"); xml.IntoElem(); xml.AddElem("UserID","luo"); xml.AddElem("UserID","lin"); xml.OutOfElem(); xml.Save("UserInfo.xml");
1.浏览特定元素
//----查看所有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(" ---tagName:%s,Data:%s-- ",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(" ---tagName:%s,Data:%s-- ",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(" ---tagName:%s,Data:%s-- ",strTagName,strData); } */
//-------------结果----------
---tagName:UserID,Data:luo--
---tagName:UserID,Data:lin—
2.修改元素
//--------把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; } } }
3.添加元素
3.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"); }
//--------原来为------
<?xml version="1.0" encoding="UTF-8" ?> <UserInfo> <UserID>luo</UserID> <UserID>lin</UserID> </UserInfo>
//---------添加后为-------------
<?xml version="1.0" encoding="UTF-8" ?> <UserInfo> <UserID>luo</UserID> <UserID>lin</UserID> <UserID>luoluo</UserID> </UserInfo>
3.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"); }
//----原来为—
<?xml version="1.0" encoding="UTF-8" ?> <UserInfo> <UserID>luo</UserID> <UserID>lin</UserID> </UserInfo>
//------修改后-----
<?xml version="1.0" encoding="UTF-8" ?> <UserInfo> <UserID>AddUserIDHead</UserID> <UserID>luo</UserID> <UserID>lin</UserID> </UserInfo>