cocos2dx tinyxml读写示例,tinyxml2读xml示例,

#include <iostream>

#include <map>
#include "cocos2d.h"
#include "../support/tinyxml2/tinyxml2.h"
#include "../Tools/tinyxml.h"
using namespace std;
using namespace cocos2d;

void writeLevel(int level);
void writeXml();
void readWithTinyXml2();
/*Xml数据的读取在游戏开发中经常会才用到,存储数据清晰,配置方便等都是他的优点,
所以我们在cocos2dx开发过程中会经常用到,所以介绍下如何使用tinyxml读写xml数据*/


/*
<?xml version="1.0" ?>
	<!--heroList-->
	<dataNodes>
	<data ID="0" Describe="abc">
		<child0>3</child0>
		<child1>720</child1>
		<child2>160</child2>
		<child3>255</child3>
		<child4>85</child4>
		<child5>1</child5>
		<child6>12</child6>
		<child7>12</child7>
		<boundRidus posX="12" posY="12" radius="30" />
		<boundRidus posX="12" posY="12" radius="30" />
		<boundRidus posX="12" posY="12" radius="30" />
	</data>
	<data ID="1" Describe="abc">
		<child0>3</child0>
		<child1>720</child1>
		<child2>160</child2>
		<child3>255</child3>
		<child4>-1</child4>
		<child5>600</child5>
		<child6>12</child6>
		<child7>12</child7>
		<boundRidus posX="32" posY="40" radius="40" />
		<boundRidus posX="17" posY="-29" radius="32" />
	</data>
</dataNodes>
*/	
void readXml()
{
	string documentPath =  "heroList.xml";
	TiXmlDocument *myDocument = new TiXmlDocument(documentPath.c_str());//"WriteTest.xml");
	if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
	{
		/*因为android无法读取assets下的xml数据文件,ios同样存在读取问题,这个具体原因是因为apk和app实质上是一个压缩包,
		压缩包的内容读取不同于一般的文件读取,因为文件已经被压缩过了,
		所以采用了读取buffer的方法,通过将buffer交给LoadMemory解析,初始化TiXmlDocument *myDocument 中的数据。
		*/
		unsigned long nLength = 0;
		char* pBuff = (char*)cocos2d::CCFileUtils::sharedFileUtils()->getFileData(CCFileUtils::sharedFileUtils()->fullPathForFilename(documentPath.c_str()).c_str(),"rt", &nLength );
		myDocument->LoadMemory( pBuff, nLength);
	}
	else
	{
		myDocument->LoadFile();
	}
	TiXmlElement *RootElement = myDocument->RootElement();/*读取根节点*/
	if(RootElement == NULL)
	{
		//LD("read from data xml error!");
	}
	TiXmlElement *FirstPerson = RootElement->FirstChildElement();/*读取根节点中的第一个节点数据*/
	while (FirstPerson)
	{
		CCLog("node name = %s",FirstPerson->Value());
		TiXmlAttribute *idAttribute = FirstPerson->FirstAttribute();/*我的文件中有两个属性值,一个是id属性*/
		TiXmlAttribute *decAttribute = idAttribute->Next();/*我的文件中有两个属性值,一个是描述属性*/
		CCLog("attrName = %s,attrValue = %d",idAttribute->Name(),atoi(idAttribute->Value()));/*打印出属性名称和属性值*/
		CCLog("attrName = %s,attrValue = %s",decAttribute->Name(),decAttribute->Value());
		TiXmlElement *firstChild = FirstPerson->FirstChildElement();/*读取第一个节点的第一个子节点*/
		map<string,int> tempMap;
		while(firstChild)
		{
			/*读取第一个节点的数据*/
			CCLog("childName = %s,childValue = %d",firstChild->Value(),atoi(firstChild->GetText()));
			tempMap.insert(map<string, int>::value_type (firstChild->Value(), atoi(firstChild->GetText())));
			firstChild = firstChild->NextSiblingElement();/*读取完第一个节点的数据,firstChild指针向后移位,读取下一个节点的数据*/
			string childName = string(firstChild->Value());
			if ( childName == "boundRidus")
			{
				break;
			}
		}
		while(firstChild)
		{
			/*我自定义的"heroList.xml"中,本子节点有三个属性,依次读取三个属性,详细的数据已经注释在代码中了*/
			TiXmlAttribute *posXAttribute = firstChild->FirstAttribute();
			TiXmlAttribute *posYAttribute = posXAttribute->Next();
			TiXmlAttribute *radiusAttr = posYAttribute->Next();
			CCLog("attriName = %s,attriValue = %d",posXAttribute->Name(),atoi(posXAttribute->Value()));
			firstChild = firstChild->NextSiblingElement();
		}
		//m_heroDatas.push_back(hData);
		FirstPerson = FirstPerson->NextSiblingElement();
	}
	writeLevel(1);
	writeXml();
}




void writeLevel(int level)
{
	/*生成xml文件,这个尽量不要在正式的程序中使用,生成的xml数据只是为了方便配置数据的人员方便配置*/
	const char * attribut[12] = {"mId","aId","pId","pAId","posX","posY","mType","explodeEffect","data8","data9","data10","data11"};
	TiXmlDocument doc ;
	TiXmlDeclaration *declare =new TiXmlDeclaration("1.0" , "","");
	doc.LinkEndChild(declare);
	doc.LinkEndChild(new TiXmlComment("levelDataInf"));
	TiXmlElement *root = new TiXmlElement("levelDataConfig");
	for (int i=0;i!=4;++i)
	{
		TiXmlElement *sub = new TiXmlElement("createMonsters");
		sub->SetAttribute("order" , i); // 向sub中添加属性
		for(int enemyC=0;enemyC!=7;++enemyC)
		{
			TiXmlElement *monster = new TiXmlElement("monster");
			for (int j=0;j!=12;++j)
			{
				monster->SetAttribute(attribut[j] , j); 
			}
			// 向sub中添加属性
			sub->LinkEndChild(monster); // 将child追加到sub中,以作为子元素
		}
		root->LinkEndChild(sub); // 将sub
	}
	doc.LinkEndChild(root);
	string documentPath = "levelTest.xml";
	doc.SaveFile(documentPath.c_str());
}

void writeXml()
{
	/*写文件,如果程序中使用了xml的进行读写数据,可以使用这个来重新写入游戏配置数据,不过本人尽量推荐使用CCUserDefault来做*/
	TiXmlDocument doc ;
	TiXmlDeclaration *declare =new TiXmlDeclaration("1.0" , "","");
	doc.LinkEndChild(declare);
	doc.LinkEndChild(new TiXmlComment("personalInfo"));

	TiXmlElement *personalInfo = new TiXmlElement("personalInfo");	
	doc.LinkEndChild(personalInfo);

	personalInfo->SetAttribute("userId", 0);	
	personalInfo->SetAttribute("loginTime", 0);
	personalInfo->SetAttribute("url", 0);		
	personalInfo->SetAttribute("scoreRecord", 0);	
	personalInfo->SetAttribute("coinTotal", 0);
	personalInfo->SetAttribute("diamondTotal", 0);
	personalInfo->SetAttribute("character", 0);		
	personalInfo->SetAttribute("knife",0);
	personalInfo->SetAttribute("sprint", 0);
	personalInfo->SetAttribute("protectCover", 9);
	personalInfo->SetAttribute("currentLevel",9);
	string documentPath = CCFileUtils::sharedFileUtils()->getWritablePath()+"writeData.xml";
	/*此路径win 在../proj.win32/Debug.win32,android在/data/data/包名/,如果是ios在*/
	doc.SaveFile(documentPath.c_str());
	readWithTinyXml2();
}

void readWithTinyXml2()
{
	/*补充一下,tinyXml2更适合用于手机应用的开发,速度更快 ,更节省内存,所以更建议大家采用tinyXml2,
	新版的cocos2d-x已经集成了tinyxml2.h 与.cpp*/
	string documentPath =  "heroList.xml";
	tinyxml2::XMLDocument *myDocument = new tinyxml2::XMLDocument();//"WriteTest.xml");
	unsigned long nSize;
	const char* pXmlBuffer = (const char*)CCFileUtils::sharedFileUtils()->getFileData(documentPath.c_str(), "rb", &nSize);
	if(NULL == pXmlBuffer)
	{
		CCLOG("can not read xml file");
		return;
	}
	myDocument->Parse(pXmlBuffer, nSize);
	delete[] pXmlBuffer;
	tinyxml2::XMLElement *RootElement = myDocument->RootElement();/*读取根节点*/
	if(!RootElement)
	{
		return;
	}
	tinyxml2::XMLElement *FirstPerson = RootElement->FirstChildElement();/*读取根节点中的第*/
	while(FirstPerson)
	{
		const tinyxml2::XMLAttribute *idAttribute = FirstPerson->FirstAttribute();
		CCLog("attribute name = %s,arrribute value = %d",idAttribute->Name(),idAttribute->IntValue());
		FirstPerson = FirstPerson->NextSiblingElement();
	}
}
cocos2d-x QQ交流群:166693678
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zanglengyu

敲碗要饭

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值