首先到http://www.firstobject.com/dn_markup.htm上面下载CMarkup
类,将CMarkup.cpp
和CMarkup.h
导入到我们的工程中就可以了。编译可能会出现问题,解决的方法是在CMarkup.cpp
的开头加上#include
,或者关闭预编译也可以。
1.创建一个XML
文档
对于创建一个XML
文档,需要实例化一个CMarkup
对象,并调用AddElem
创建根元素。在这个位置,如果你调用AddElem("School")
,文档会简单的装一个空元素. 然后调用AddChildElem
在根元素的下面创建元素。
|
CMarkup xml; xml.SetDoc("\r\n"); xml.AddElem("School"); xml.IntoElem(); xml.AddElem("Department","Automation"); xml.AddElem("Department","Computer"); xml.AddElem("Department","Math"); xml.AddElem("Department","English"); xml.OutOfElem(); xml.Save("School.xml"); |
效果如下:
|
<?xml version="1.0" encoding="UTF-8"?> <School> <Department>Automation</Department> <Department>Computer</Department> <Department>Math</Department> <Department>English</Department> </School> |
2.浏览特定元素
|
CMarkup xml; xml.Load("School.xml"); BOOL bFind=TRUE; xml.ResetMainPos(); while(xml.FindChildElem("Department")) { CString strTagName=_T(""); CString strData=_T(""); strTagName=xml.GetChildTagName(); //此时节点是父节点 strData=xml.GetChildData(); TRACE("\n---tagName:%s,Data:%s--\n",strTagName,strData); } |
//***********************这样也可以*****************************/
|
CMarkup xml; xml.Load("School.xml"); BOOL bFind = TRUE; xml.ResetMainPos(); while (xml.FindChildElem("Department")) { xml.IntoElem(); CString strTagName = _T(""); CString strData = _T(""); strTagName = xml.GetTagName(); strData = xml.GetData(); TRACE("\n---tagName:%s,Data:%s--\n",strTagName,strData); xml.OutOfElem(); } |
//*********************或者这样也可以*************************/
|
CMarkup xml; xml.Load("School.xml"); BOOL bFind = TRUE; xml.ResetMainPos(); xml.FindElem(); //School xml.IntoElem(); while (xml.FindElem("Department")) { CString strTagName = _T(""); CString strData = _T(""); strTagName = xml.GetTagName(); strData = xml.GetData(); TRACE("\n---tagName:%s,Data:%s--\n",strTagName,strData); } |
//***********结果**************
|
---tagName:Department,Data:Automation-- ---tagName:Department,Data:Computer-- ---tagName:Department,Data:Math-- ---tagName:Department,Data:English-- |
3.修改元素
//把Department
为"English
"改为"Chinese
"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
CMarkup xml; BOOL bLoadXml=FALSE; bLoadXml=xml.Load("School.xml"); if(bLoadXml) { CString str=_T(""); xml.ResetMainPos(); xml.FindElem(); xml.IntoElem(); while(xml.FindElem("Department")) { str=xml.GetData(); if(str=="English") { xml.SetData("Chinese"); xml.Save("School.xml"); break; } } } |
效果如下:
|
<?xml version="1.0" encoding="UTF-8"?> <School> <Department>Automation</Department> <Department>Computer</Department> <Department>Math</Department> <Department>Chinese</Department> </School> |
4.添加
4.1 添加在最后面(用AddElem
)
|
CMarkup xml; BOOL bLoadXml=FALSE; bLoadXml=xml.Load("School.xml"); if(bLoadXml) { xml.ResetMainPos(); xml.FindElem(); xml.IntoElem(); xml.AddElem("Department","SiQinghua"); xml.OutOfElem(); xml.Save("School.xml"); } |
效果如下:
|
<?xml version="1.0" encoding="UTF-8"?> <School> <Department>Automation</Department> <Department>Computer</Department> <Department>Math</Department> <Department>Chinese</Department> <Department>SiQinghua</Department> </School> |
4.2 添加在最前面(用InsertElem
)
|
CMarkup xml; BOOL bLoadXml=FALSE; bLoadXml=xml.Load("School.xml"); if(bLoadXml) { xml.ResetMainPos(); xml.FindElem(); xml.IntoElem(); xml.AddElem("Department","SiQinghua"); xml.OutOfElem(); xml.Save("School.xml"); } |
效果如下:
|
<?xml version="1.0" encoding="UTF-8"?> <School> <Department>NARI</Department> <Department>Automation</Department> <Department>Computer</Department> <Department>Math</Department> <Department>Chinese</Department> <Department>SiQinghua</Department> </School> |
5.删除
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
CMarkup xml; BOOL bLoadXml=FALSE; bLoadXml=xml.Load("School.xml"); if(bLoadXml) { BOOL bFind=TRUE; xml.ResetMainPos(); while(bFind) { bFind=xml.FindChildElem("Department"); if(bFind) { CString strData=_T(""); strData=xml.GetChildData(); //此时节点还是父节点 if("SiQinghua"==strData) { xml.RemoveChildElem(); xml.Save("School.xml"); break; } } } } |
效果如下:
|
<?xml version="1.0" encoding="UTF-8"?> <School> <Department>NARI</Department> <Department>Automation</Department> <Department>Computer</Department> <Department>Math</Department> <Department>Chinese</Department> </School> |
上面的例子都是不带属性的,下面举一个带有属性的例子。
1.生成XML
文档
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
CMarkup xml; xml.SetDoc("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); xml.AddElem("UserInfo"); xml.IntoElem(); for(int i=0;i<10;i++) { CString strID=_T(""); CString strPwd=_T(""); srand(UINT(time(NULL)+i)); //产生一个随机数 int nLevel=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("UserInfo.xml"); |
效果如下:
|
<?xml version="1.0" encoding="UTF-8"?> <UserInfo> <User UserID="UserID00" UserPwd="UserPwd00" UserLevel="85"/> <User UserID="UserID01" UserPwd="UserPwd01" UserLevel="88"/> <User UserID="UserID02" UserPwd="UserPwd02" UserLevel="92"/> <User UserID="UserID03" UserPwd="UserPwd03" UserLevel="95"/> <User UserID="UserID04" UserPwd="UserPwd04" UserLevel="98"/> <User UserID="UserID05" UserPwd="UserPwd05" UserLevel="101"/> <User UserID="UserID06" UserPwd="UserPwd06" UserLevel="105"/> <User UserID="UserID07" UserPwd="UserPwd07" UserLevel="108"/> <User UserID="UserID08" UserPwd="UserPwd08" UserLevel="111"/> <User UserID="UserID09" UserPwd="UserPwd09" UserLevel="115"/> </UserInfo> |
2.查找特定属性
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
CMarkup xml; BOOL bLoadXml=FALSE; BOOL bFind=FALSE; bLoadXml=xml.Load("UserInfo.xml"); if(bLoadXml) { CString strID; CString strPwd; 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=="UserID02" && strPwd=="UserPwd02") { bFind=TRUE; break; } } } if(bFind==TRUE) TRACE("\n---find---\n"); else TRACE("\n---not find---\n"); |
3.修改特定属性
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
CMarkup xml; BOOL bFind=FALSE; BOOL bLoadXml=FALSE; bLoadXml=xml.Load("UserInfo.xml"); if(bLoadXml) { CString strID; CString strPwd; 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=="UserID02" && strPwd=="UserPwd02") { bFind=TRUE; xml.SetChildAttrib("UserID",strID+CString("Modified")); xml.Save("UserInfo.xml"); break; } } } if(bFind==TRUE) TRACE("\n---find---\n"); else TRACE("\n---not find---\n"); |
效果如下:
|
<?xml version="1.0" encoding="UTF-8"?> <UserInfo> <User UserID="UserID00" UserPwd="UserPwd00" UserLevel="85"/> <User UserID="UserID01" UserPwd="UserPwd01" UserLevel="88"/> <User UserID="UserID02Modified" UserPwd="UserPwd02" UserLevel="92"/> <User UserID="UserID03" UserPwd="UserPwd03" UserLevel="95"/> <User UserID="UserID04" UserPwd="UserPwd04" UserLevel="98"/> <User UserID="UserID05" UserPwd="UserPwd05" UserLevel="101"/> <User UserID="UserID06" UserPwd="UserPwd06" UserLevel="105"/> <User UserID="UserID07" UserPwd="UserPwd07" UserLevel="108"/> <User UserID="UserID08" UserPwd="UserPwd08" UserLevel="111"/> <User UserID="UserID09" UserPwd="UserPwd09" UserLevel="115"/> </UserInfo> |
参考链接
在VC中用CMarkup类操纵XML