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