CMarkup类解析xml文件 例子

前端之家收集整理的这篇文章主要介绍了CMarkup类解析xml文件 例子前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

CMarkup类解析xml文件

将CMarkup.cpp和CMarkup.h导入到我们的工程中就可以了。编译可能会出现问题,解决方法是在CMarkup.cpp的开头加上#include <stdafx.h>,或者关闭预编译也可以。

1、创建一个XML文档

1 CMarkup xml;

2 xml.Load("School.xml");

3 BOOL bFind=TRUE;

4 xml.ResetMainPos();

5

6while(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 }

//***********************这样也可以*****************************/

1 CMarkup xml;

2 xml.Load("School.xml");

3 BOOL bFind=TRUE;

4 xml.ResetMainPos();

5

6while(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,strData);

14 xml.OutOfElem();

15 }

//***********************或者这样也可以***************************/

1 CMarkup xml;

2 xml.Load("School.xml");

3 BOOL bFind=TRUE;

4 xml.ResetMainPos();

5 xml.FindElem();//School

6 xml.IntoElem();

7

8while(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,strData);

15 }

//***********结果**************

---tagName:Department,Data:Automation--

---tagName:Department,Data:Computer--

---tagName:Department,Data:Math--

---tagName:Department,Data:English--

3、修改元素

//把Department为”English“改为”Chinese“

1 CMarkup xml;

2 BOOL bLoadXml=FALSE;

3 bLoadXml=xml.Load("School.xml");

4

5if(bLoadXml)

6 {

7 CString str=_T("");

8 xml.ResetMainPos();

9 xml.FindElem();

10 xml.IntoElem();

11while(xml.FindElem("Department"))

12 {

13 str=xml.GetData();

14if(str=="English")

15 {

16 xml.SetData("Chinese");

17 xml.Save("School.xml");

18break;

19 }

20 }

21 }

效果如下:

1 <?xml version="1.0" encoding="UTF-8"?>

2 <School>

3 <Department>Automation</Department>

4 <Department>Computer</Department>

5 <Department>Math</Department>

6 <Department>Chinese</Department>

7 </School>

8

对于创建一个XML文档,需要实例化一个CMarkup对象,并调用AddElem创建根元素。在这个位置,如果你调用AddElem("School"),文档会简单的装一个空元素<School/>.然后调用AddChildElem在根元素的下面创建元素。

1 CMarkup xml;

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");

效果如下:

1 <?xml version="1.0" encoding="UTF-8"?>

2 <School>

3 <Department>Automation</Department>

4 <Department>Computer</Department>

5 <Department>Math</Department>

6 <Department>English</Department>

7 </School>

8

2、浏览特定元素

4、添加

4、1 添加在最后面(用AddElem)

1 CMarkup xml;

2 BOOL bLoadXml=FALSE;

3 bLoadXml=xml.Load("School.xml");

4if(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 }

效果如下:

1 <?xml version="1.0" encoding="UTF-8"?>

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)

1 CMarkup xml;

2 BOOL bLoadXml=FALSE;

3 bLoadXml=xml.Load("School.xml");

4if(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 }

效果如下:

1 <?xml version="1.0" encoding="UTF-8"?>

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、删除

1 CMarkup xml;

2 BOOL bLoadXml=FALSE;

3 bLoadXml=xml.Load("School.xml");

4if(bLoadXml)

5 {

6 BOOL bFind=TRUE;

7 xml.ResetMainPos();

8while(bFind)

9 {

10 bFind=xml.FindChildElem("Department");

11if(bFind)

12 {

13 CString strData=_T("");

14 strData=xml.GetChildData();//此时节点还是父节点

15if("SiQinghua"==strData)

16 {

17 xml.RemoveChildElem();

18 xml.Save("School.xml");

19break;

20 }

21 }

22 }

23 }

效果如下:

1<?xml version="1.0" encoding="UTF-8"?>

2<School>

3<Department>NARI</Department>

4<Department>Automation</Department>

5<Department>Computer</Department>

6<Department>Math</Department>

7<Department>Chinese</Department>

8</School>

上面的例子都是不带属性的,下面举一个带有属性的例子。

1、生成XML文档

1 CMarkup xml;

2 xml.SetDoc("<?xml version=\"1.0\"encoding=\"UTF-8\"?>");

3 xml.AddElem("UserInfo");

4 xml.IntoElem();

5for(inti=0;i<10;i++)

6 {

7 CString strID=_T("");

8 CString strPwd=_T("");

9

10 srand(UINT(time(NULL)+i));//产生一个随机

11intnLevel=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");

效果如下:

1 <?xml version="1.0" encoding="UTF-8"?>

2 <UserInfo>

3 <UserUserID="UserID00"UserPwd="UserPwd00"UserLevel="85"/>

4 <UserUserID="UserID01"UserPwd="UserPwd01"UserLevel="88"/>

5 <UserUserID="UserID02"UserPwd="UserPwd02"UserLevel="92"/>

6 <UserUserID="UserID03"UserPwd="UserPwd03"UserLevel="95"/>

7 <UserUserID="UserID04"UserPwd="UserPwd04"UserLevel="98"/>

8 <UserUserID="UserID05"UserPwd="UserPwd05"UserLevel="101"/>

9 <UserUserID="UserID06"UserPwd="UserPwd06"UserLevel="105"/>

10 <UserUserID="UserID07"UserPwd="UserPwd07"UserLevel="108"/>

11 <UserUserID="UserID08"UserPwd="UserPwd08"UserLevel="111"/>

12 <UserUserID="UserID09"UserPwd="UserPwd09"UserLevel="115"/>

13 </UserInfo>

14

2、查找特定属性

1 CMarkup xml;

2 BOOL bLoadXml=FALSE;

3 BOOL bFind=FALSE;

4 bLoadXml=xml.Load("UserInfo.xml");

5if(bLoadXml)

6 {

7 CString strID;

8 CString strPwd;

9 xml.ResetMainPos();

10 xml.FindElem();//UserInfo

11while(xml.FindChildElem("User"))

12 {

13 strID=xml.GetChildAttrib("UserID");

14 strPwd=xml.GetChildAttrib("UserPwd");

15 TRACE("\n---id:%s,pwd:%s---\n",strID,strPwd);

16if(strID=="UserID02"&&strPwd=="UserPwd02")

17 {

18 bFind=TRUE;

19break;

20 }

21 }

22 }

23if(bFind==TRUE)

24 TRACE("\n---find---\n");

25else

26 TRACE("\n---not find---\n");

3、修改特定属性

1 CMarkup xml;

2 BOOL bFind=FALSE;

3 BOOL bLoadXml=FALSE;

4 bLoadXml=xml.Load("UserInfo.xml");

5if(bLoadXml)

6 {

7 CString strID;

8 CString strPwd;

9 xml.ResetMainPos();

10 xml.FindElem();//UserInfo

11while(xml.FindChildElem("User"))

12 {

13 strID=xml.GetChildAttrib("UserID");

14 strPwd=xml.GetChildAttrib("UserPwd");

15 TRACE("\n--id:%s,pwd:%s--\n",strPwd);

16if(strID=="UserID02"&&strPwd=="UserPwd02")

17 {

18 bFind=TRUE;

19 xml.SetChildAttrib("UserID",strID+CString("Modified"));

20 xml.Save("UserInfo.xml");

21break;

22 }

23 }

24 }

25if(bFind==TRUE)

26 TRACE("\n---find---\n");

27else

28 TRACE("\n---not find---\n");

效果如下:

1 <?xml version="1.0" encoding="UTF-8"?>

2 <UserInfo>

3 <UserUserID="UserID00"UserPwd="UserPwd00"UserLevel="85"/>

4 <UserUserID="UserID01"UserPwd="UserPwd01"UserLevel="88"/>

5 <UserUserID="UserID02Modified"UserPwd="UserPwd02"UserLevel="92"/>

6 <UserUserID="UserID03"UserPwd="UserPwd03"UserLevel="95"/>

7 <UserUserID="UserID04"UserPwd="UserPwd04"UserLevel="98"/>

8 <UserUserID="UserID05"UserPwd="UserPwd05"UserLevel="101"/>

9 <UserUserID="UserID06"UserPwd="UserPwd06"UserLevel="105"/>

10 <UserUserID="UserID07"UserPwd="UserPwd07"UserLevel="108"/>

11 <UserUserID="UserID08"UserPwd="UserPwd08"UserLevel="111"/>

12 <UserUserID="UserID09"UserPwd="UserPwd09"UserLevel="115"/>

13 </UserInfo>

原文链接:https://www.f2er.com/xml/294092.html

猜你在找的XML相关文章