cocos2d-x 3.2 XML读取第一章(新手必看)

以下内容通过XML,完成读取操作。清晰的列出每一步获取子节点的过程,

***如果觉得冗余,老手请直接跳到第二章开始看。第二章将对本章的内容进行优化处理。


首先:读取下面这个XML

<?xml version="1.0" encoding="UTF-8"?>  
<Class name="计算机软件班">  
    <Students>  
        <student name="张三" studentNum="13031001" sex="男" age="22">  
            <phone>88208888</phone>  
            <address>西安市太白南路二号</address>  
        </student>  
        <student name="李四" studentNo="13031002" sex="男" age="20">  
            <phone type="土豪金">88206666</phone>  
            <address>西安市光华路</address>  
        </student>  
    </Students>  
	
</Class>  

如何读取,源码:

先导入3.2 封装的 XML头文件

#include "tinyxml2/tinyxml2.h"
using namespace tinyxml2;

在init()方法:写入


	tinyxml2::XMLDocument* homework	                        =	new tinyxml2::XMLDocument();
	homework->LoadFile("homework.xml");
	//获取根节点
	XMLElement* root					=	homework->RootElement();
	//获取根节点的名称、值
	log("root key:%s,root val :%s",root->Value(),root->GetText());
	//获取根节点的属性
	const XMLAttribute* rootpro				=	root->FirstAttribute();
	log("root attrName: %s , root attrVal : %s",rootpro->Name(),rootpro->Value());
	//获取第一个子节点
	XMLElement* students					=	root->FirstChildElement();
	//获取第一个子节的名称和值,并指向下一个节点
	log("students name : %s , students val : %s",students->Value(),students->GetText());
	//获取第二个节点的名、值:
	XMLElement* studentOneEle				=	students->FirstChildElement();
	//注意下面的获取,节点的name调用的是Value的接口
	log("studentOneEle name:%s , studentOneEle val:%s" ,studentOneEle->Value(),studentOneEle->GetText());
	//获取属性
	const XMLAttribute* studentOneEleAttr			=	studentOneEle->FirstAttribute();
	//猥琐的获取第一个节(学)点(生)的各个属性
	log("fristattr name1 :%s , firstattr val1: %s",studentOneEleAttr->Name(),studentOneEleAttr->Value());
	studentOneEleAttr					=	studentOneEleAttr->Next();
	log("fristattr name2 :%s , firstattr val2: %s",studentOneEleAttr->Name(),studentOneEleAttr->Value());
	studentOneEleAttr					=	studentOneEleAttr->Next();
	log("fristattr name3 :%s , firstattr val3: %s",studentOneEleAttr->Name(),studentOneEleAttr->Value());
	studentOneEleAttr					=	studentOneEleAttr->Next();
	log("fristattr name4 :%s , firstattr val4: %s",studentOneEleAttr->Name(),studentOneEleAttr->Value());
	//获取phone节点
	XMLElement* phone					=	studentOneEle->FirstChildElement();
	log("phone name : %s, phone val :%s",phone->Value(),phone->GetText());
	//获取address节点
	XMLElement* address					=	phone->NextSiblingElement();
	log("address name : %s, address val :%s",address->Value(),address->GetText());
	//通过NextSiblingElement()获取同级兄弟节点
	XMLElement* studentTwoEle				=	studentOneEle->NextSiblingElement();
	log("studentTwoEle name:%s , studentTwoEle val:%s" ,studentTwoEle->Value(),studentOneEle->GetText());
	const XMLAttribute* studentTwoAttr			=	studentTwoEle->FirstAttribute();
	//分别获取第二个学生的属性
	log("studentTwoAttr name1 :%s , studentTwoAttr val1: %s",studentTwoAttr->Name(),studentTwoAttr->Value());
	studentTwoAttr						=	studentTwoAttr->Next();
	log("studentTwoAttr name2 :%s , studentTwoAttr val2: %s",studentTwoAttr->Name(),studentTwoAttr->Value());
	studentTwoAttr						=	studentTwoAttr->Next();
	log("studentTwoAttr name3 :%s , studentTwoAttr val3: %s",studentTwoAttr->Name(),studentTwoAttr->Value());
	studentTwoAttr						=	studentTwoAttr->Next();
	log("studentTwoAttr name4 :%s , studentTwoAttr val4: %s",studentTwoAttr->Name(),studentTwoAttr->Value());
	//再猥琐的拿到他的电话
	XMLElement* phone2					=	studentTwoEle->FirstChildElement();
	log("phone2 name:%s , phone2 var :%s" ,phone2->Value(),phone2->GetText());
	//电话的类型
	const XMLAttribute* phone2Attr				=	phone2->FirstAttribute();
	log("phone name: %s, phone val: %s ", phone2Attr->Name(),phone2Attr->Value());
	//再拿到家庭住址
	XMLElement* address2					=	phone2->NextSiblingElement();
	log("address2 name : %s, address2 val : %s",address2->Name(),address2->GetText());  
<span style="font-size: 12px;"><span style="white-space: pre;">	</span>delete homework;</span>


关于TinyXML2基本使用:

XML文档的结构:

TinyXml2实现的时DOM访问模型,主要类间的关系如下图所示:

XMLBase:其他类的基类,是个抽象类

XMLNode:表示一个节点,包含一般方法,如访问自节点、兄弟节点、编辑自身、编辑子节点

XMLDocument:表示整个XML文档,不对应其中某个特定的节点。

XMLElement:表示元素节点,可以包含子节点和XMLAttribute

XMLComment:表示注释

XMLDeclaration:表示声明

XMLText:表示文本节点

XMLUnknown:表示未知节点,通常是出错了

XMLAttribute:表示一个元素的属性



猜你喜欢

转载自blog.csdn.net/yanghaojian/article/details/38299381
今日推荐