cocos2dx 3.2 读写XML,基于tinyxml2封装的易使用,更灵活的XML接口。中文国际化。

cocos2dx 3.2 读写XML,基于tinyxml2封装的易使用,更灵活的XML接口。中文国际化。,第1张

概述简介 cocos2dx 3.2读写XML使用的是tinyxml2库。tinyxml2是一个轻量的解析XML的开源库,C++编写,跨平台,内存占用很小。解析文件时,在内存中生成DOM模型,即文档对象模型,遍历这颗树读取想要的数据。 UserDefault类是一个引擎封装好的XML读取类,但是使用这个类读写的XML的文件名固定为UserDefault.xml,并且不能灵活定义文档结构。所以对有特殊XM 简介

cocos2dx 3.2读写XML使用的是tinyxml2库。tinyxml2是一个轻量的解析XML的开源库,C++编写,跨平台,内存占用很小。解析文件时,在内存中生成DOM模型,即文档对象模型,遍历这颗树读取想要的数据。

UserDefault类是一个引擎封装好的XML读取类,但是使用这个类读写的XML的文件名固定为UserDefault.xml,并且不能灵活定义文档结构。所以对有特殊XML需求的项目中,就不太适用了。而如果在项目中直接使用tinyxml2库,感觉还不是很方便,所以基于tinyxml2又封装了一个XMLManager类,使用起来更方便,并且能够灵活创建、读取不同结构的XML文件。

XMLManager类同时提供了一个中文资源国际化的方法,一般小项目可能会用到,把中文存到XML文件中读取。

类图
代码 tinyxml2的使用方法可以参照一下如下代码,代码的注释以及使用方法也写得比较详细。 头文件:
/* * WWXMLManager.h * *  Created on: 2014年5月20日 *      Author: wly */#ifndef _WWXMLMANAGER_H_#define _WWXMLMANAGER_H_#include "cocos2d.h"//#include "tinyxml2.h"#include "tinyxml2/tinyxml2.h"/* *   属性类,这里属性是只读的,修改请使用WWXMLNode类 *   <express name="test" value="nothing">This is a test!</express> *   name 和 value 就是结点的属性 */class WWXMLAttribute{public:	WWXMLAttribute(const tinyxml2::XMLAttribute *pXMLAttribute);	~WWXMLAttribute();	//下一个属性值	WWXMLAttribute next();	//获取属性名称	const char* getname();	//获取string类型属性值	const char* value();	//获取int类型属性值	int intValue();	//获取bool类型属性值	bool boolValue();	//获取float类型属性值	float floatValue();	//获取double类型属性值	double doubleValue();	//返回是否是空	bool isNulL();private:	//文档属性对象	const tinyxml2::XMLAttribute *m_pXMLAttribute;};/* *  节点类,封装对节点的各种 *** 作. *  如下类型节点 *	<example name="ok",flag="1">text</example> *    *  example是 nodename *  name和flag是attribute *  text是 nodeValue */class WWXMLNode{public:	WWXMLNode(tinyxml2::XMLElement *pXMLElem);	WWXMLNode(tinyxml2::XMLElement *pXMLElem,tinyxml2::XMLdocument *pXMLdocument);	~WWXMLNode();	//增加子节点	WWXMLNode addChildNode(const char* name);	//查找子节点,默认返回第一个子节点	WWXMLNode findChildNode(const char* name = NulL);	//查找下一个兄弟节点,默认返回下面第一个兄弟节点	WWXMLNode findSlibingNode(const char* name =NulL);	//查找上一个兄弟节点,默认返回上面第一个兄弟节点	WWXMLNode preSlibingNode(const char* name = NulL);	//设置节点属性值	voID setAttributeValue(const char* name,const char* value);	//获取指定的属性值	const char* getAttributeValue(const char* name);	//删除一个指定名称的属性值	voID deleteAttribute(const char* name);	//设置节点名称	voID setNodename(const char* name);	//获取节点名称	const char* getNodename();	//设置节点值	voID setNodeValue(const char* value);	//获取节点值	const char* getNodeValue();	//获取属性,默认返回第一个属性	WWXMLAttribute firstAttribute(const char* name = NulL);	//删除本节点	voID removeSelf();	//删除所有子节点	voID removeAllChildNode();	//是否是空节点	bool isNulL();private:	tinyxml2::XMLdocument *m_pXMLdocument;	tinyxml2::XMLElement *m_pXMLElem;};/* * XML管理类,使用tinyxml2封装了 *** 作xml的细节,需要配合WWXMLNode类使用。 * 封装了CCUserDefault提供的功能 * * example: * WWXMLManager myXML; * myXML.createXMLfile("myXML.xml","TestXMLManager"); * WWXMLNode itemNode = myXML.getXMLRootNode().addChildNode("item"); * itemNode.setAttributeValue("flag","true"); * myXML.saveXMLfile(); */class WWXMLManager{public:	WWXMLManager();	WWXMLManager(const char* strXMLPath);	~WWXMLManager(voID);	//加载xml文件,utf-8格式文件	bool loadXMLfile(const char* strXmlPath);	//获取xml文件根目录值	const char* getXMLRootkeyvalue();	//获取根结点	WWXMLNode getXMLRootNode();	//创建XML文件,默认为utf-8编码	bool createXMLfile(const char* strfilename,const char* rootNode="root");	//保存文件,修改后需要调用此方法	bool saveXMLfile();	/*	 *  以下方法适用于引擎提供的默认XML文件,可使用getXMLfilePath获取文件路径	 *  对应于CCUserDefault类	 */public:	//根据key获取bool的值,当key值不存在时,返回defaultValue	static bool getDefaultXMLBoolForKey(const char* pKey);	static bool getDefaultXMLBoolForKey(const char* pKey,bool defaultValue);	static int getDefaultXMlintegerForKey(const char* pKey);	static int getDefaultXMlintegerForKey(const char* pKey,int defaultValue); 	static float getDefaultXMLfloatForKey(const char* pKey);	static float getDefaultXMLfloatForKey(const char* pKey,float defaultValue);	static double getDefaultXMLDoubleForKey(const char* pKey);	static double getDefaultXMLDoubleForKey(const char* pKey,double defaultValue);	static std::string getDefaultXMLStringForKey(const char* pKey);	static std::string getDefaultXMLStringForKey(const char* pKey,const std::string & defaultValue);	//根据key值以bool设置值	static voID setDefaultXMLBoolForKey(const char* pKey,bool value);    static voID setDefaultXMlintegerForKey(const char* pKey,int value);    static voID setDefaultXMLfloatForKey(const char* pKey,float value);    static voID setDefaultXMLDoubleForKey(const char* pKey,double value);    static voID setDefaultXMLStringForKey(const char* pKey,const std::string & value);    static voID purgeSharedUserDefault();	//获取默认的XML文件路径    const static std::string& getDefaultXMLfilePath();	//xml文件是否存在	static bool isXmlfileExist(const char* strfilePath);	/*	 *   说明:获取项目的字符串资源,类似如下的文件格式	 *   参数:键值,段名值。	 *   例子:	 *   <?xml version="1.0" enCoding="utf-8"?>	 *   <resources>	 *       <HallScene>	 *           <string name="Str_app_name">项目名称</string> 	 *           <string name="Str_chujiRoom">初级房</string> 	 *       </HallScene>	 *       <GameScene>	 *           <string name="Str_app_name">项目名称</string> 	 *       </GameScene>	 *    </resources>	 *	 */	static std::string getWWStringFromXML(const std::string &strKey,const std::string &strSection);private:	tinyxml2::XMLdocument *m_pXMLdocument;	//文件路径,每次load时改变	std::string m_strXMLfilePath;};#endif

实现:
/* * WWXMLManager.h * *  Created on: 2014年5月20日 *      Author: wly */#include "WWXMLManager.h"#include "../WWMacros.h"#include "WW_GBK_TO_UTF8.h"USING_NS_CC;WWXMLAttribute::WWXMLAttribute(const tinyxml2::XMLAttribute *pXMLAttribute):m_pXMLAttribute(pXMLAttribute){	}WWXMLAttribute::~WWXMLAttribute(){}WWXMLAttribute WWXMLAttribute::next(){	if (m_pXMLAttribute)	{		return m_pXMLAttribute->Next();	}	return NulL;}const char* WWXMLAttribute::getname(){	if (m_pXMLAttribute)	{		const char* pname = m_pXMLAttribute->name();		if (pname)		{			return pname;		}	}	return "";}const char* WWXMLAttribute::value(){	if (m_pXMLAttribute)	{		const char* pValue = m_pXMLAttribute->Value();		if (pValue)		{			return pValue;		}	}	return "";}int WWXMLAttribute::intValue(){	if (m_pXMLAttribute)	{		return m_pXMLAttribute->IntValue();	}	return -1;}bool WWXMLAttribute::boolValue(){	if (m_pXMLAttribute)	{		return m_pXMLAttribute->BoolValue();	}	return false;}float WWXMLAttribute::floatValue(){	if (m_pXMLAttribute)	{		return m_pXMLAttribute->floatValue();	}	return -1;}double WWXMLAttribute::doubleValue(){	if (m_pXMLAttribute)	{		return m_pXMLAttribute->DoubleValue();	}	return -1;}bool WWXMLAttribute::isNulL(){	return NulL == m_pXMLAttribute;}WWXMLNode::WWXMLNode(tinyxml2::XMLElement *pElem):m_pXMLElem(pElem),m_pXMLdocument(NulL){}WWXMLNode::WWXMLNode(tinyxml2::XMLElement *pElem,tinyxml2::XMLdocument *pdocument):m_pXMLElem(pElem),m_pXMLdocument(pdocument){	}WWXMLNode::~WWXMLNode(){}WWXMLNode WWXMLNode::addChildNode(const char* name){	if (m_pXMLElem && m_pXMLdocument)	{		tinyxml2::XMLElement *pElem = m_pXMLdocument->NewElement(name);		if (pElem)		{			m_pXMLElem->linkEndChild(pElem);			WWXMLNode node(pElem,m_pXMLdocument);			return node;		}	}	return NulL;}WWXMLNode WWXMLNode::findChildNode(const char* name /* = NulL */){	if (m_pXMLElem && m_pXMLdocument)	{		if (NulL == name)		{			WWXMLNode node(m_pXMLElem->FirstChildElement(),m_pXMLdocument);			return node;		}		else		{			WWXMLNode node(m_pXMLElem->FirstChildElement(name),m_pXMLdocument);			return node;			}	}	return NulL;}WWXMLNode WWXMLNode::findSlibingNode(const char* name /* =NulL */){	if (m_pXMLElem && m_pXMLdocument)	{		if (NulL == name)		{			WWXMLNode node(m_pXMLElem->NextSiblingElement(),m_pXMLdocument);			return node;		}		else		{			WWXMLNode node(m_pXMLElem->NextSiblingElement(name),m_pXMLdocument);			return node;			}	}	return NulL;}WWXMLNode WWXMLNode::preSlibingNode(const char* name /* =NulL */){	if (m_pXMLElem && m_pXMLdocument)	{		if (NulL == name)		{			WWXMLNode node(m_pXMLElem->PrevIoUsSiblingElement(),m_pXMLdocument);			return node;		}		else		{			WWXMLNode node(m_pXMLElem->PrevIoUsSiblingElement(name),m_pXMLdocument);			return node;			}	}	return NulL;}voID WWXMLNode::setAttributeValue(const char* name,const char* value){	if (m_pXMLElem && NulL != name)	{		m_pXMLElem->SetAttribute(name,value);	}}const char* WWXMLNode::getAttributeValue(const char* name){	if (m_pXMLElem && NulL != name)	{		const char* pname = m_pXMLElem->Attribute(name);		if (NulL == pname)		{			return "";		}		return pname;	}	return "";}voID WWXMLNode::deleteAttribute(const char* name){	if (m_pXMLElem)	{		m_pXMLElem->DeleteAttribute(name);	}}voID WWXMLNode::setNodename(const char* name){	if (m_pXMLElem && NulL != name)	{		m_pXMLElem->Setname(name);	}}const char* WWXMLNode::getNodename(){	if (m_pXMLElem)	{		const char* pname = m_pXMLElem->name();		if (NulL == pname)		{			return "";		}		return pname;	}	return "";}voID WWXMLNode::setNodeValue(const char* value){	if (m_pXMLElem && m_pXMLdocument)	{		tinyxml2::XMLText *pText = m_pXMLdocument->NewText(value);		m_pXMLElem->linkEndChild(pText);	}}const char* WWXMLNode::getNodeValue(){	if (m_pXMLElem)	{		const char* pValue = m_pXMLElem->GetText();		if (NulL == pValue)		{			return "";		}		return pValue;	}	return "";}WWXMLAttribute WWXMLNode::firstAttribute(const char* name /*= NulL*/){	if (m_pXMLElem)	{		if (NulL == name)		{			return m_pXMLElem->FirstAttribute();		}		else		{			WWXMLAttribute attribute = m_pXMLElem->FirstAttribute();			while (!attribute.isNulL())			{				if (strcmp(name,attribute.getname()) == 0)				{					return attribute;				}				attribute = attribute.next();			}		}			}	return NulL;}voID WWXMLNode::removeSelf(){	if (m_pXMLElem)	{		if (m_pXMLElem->Parent())		{			m_pXMLElem->Parent()->DeleteChild((tinyxml2::XMLNode*)m_pXMLElem);		}	}}voID WWXMLNode::removeAllChildNode(){	if (m_pXMLElem)	{		m_pXMLElem->DeleteChildren();	}}bool WWXMLNode::isNulL(){	return m_pXMLElem == NulL;}WWXMLManager::WWXMLManager():m_pXMLdocument(NulL),m_strXMLfilePath(""){}WWXMLManager::WWXMLManager(const char* strXMLPath):m_pXMLdocument(NulL),m_strXMLfilePath(strXMLPath){	loadXMLfile(strXMLPath);}WWXMLManager::~WWXMLManager(voID){	if (m_pXMLdocument)	{		delete m_pXMLdocument;	}}bool WWXMLManager::loadXMLfile(const char* strXmlPath){	//加载同一文件时,直接返回	if (strcmp(m_strXMLfilePath.c_str(),strXmlPath) == 0)	{		return true;	}	m_strXMLfilePath = strXmlPath;	//首先置空,防止重复加载	if (m_pXMLdocument)	{		delete m_pXMLdocument;		m_pXMLdocument = NulL;	}	m_pXMLdocument = new tinyxml2::XMLdocument();	if (NulL == m_pXMLdocument)	{		return false;	}	//使用文件内容加载,直接加载可能会有兼容性问题,字符流内存需要手动释放	std::string strXmlBuffer = CCfileUtils::getInstance()->getStringFromfile(strXmlPath);	if (strXmlBuffer.empty())	{		cclOG("WWXMLManager::%s file data is empty!",strXmlPath);		return false;	}	//解析XML字符流	if (tinyxml2::XML_SUCCESS != m_pXMLdocument->Parse(strXmlBuffer.c_str(),strXmlBuffer.length()))	{		cclOG("WWXMLManager::%s Parse data error!",strXmlPath);		return false;	}	return true;}const char* WWXMLManager::getXMLRootkeyvalue(){	if (m_pXMLdocument)	{		return getXMLRootNode().getNodename();	}	return "";}WWXMLNode WWXMLManager::getXMLRootNode(){	if (m_pXMLdocument)	{		WWXMLNode node(m_pXMLdocument->RootElement(),m_pXMLdocument);		return node;	}	return NulL;}bool WWXMLManager::createXMLfile(const char* strfilename,const char* rootNode/* ="root" */){	//利用tinyxml2方法创建XML文件	tinyxml2::XMLdocument *pDoc = new tinyxml2::XMLdocument(); 	if (NulL == pDoc)  	{  		return false;  	}  	bool bRet = false;	do 	{		tinyxml2::XMLDeclaration *pDeclaration = pDoc->NewDeclaration(NulL);		if (NulL == pDeclaration)  		{  			bRet = false;  			break;		}  		pDoc->linkEndChild(pDeclaration); 		tinyxml2::XMLElement *pRootEle = pDoc->NewElement(rootNode);  		if (NulL == pRootEle)  		{  			bRet = false;  			break;  		}  		pDoc->linkEndChild(pRootEle);  		if (tinyxml2::XML_SUCCESS != pDoc->Savefile(strfilename))		{			bRet = false;  			break;		}		bRet = true;	} while (0);	if(pDoc)	{		delete pDoc;	}	//创建成功后加载	loadXMLfile(strfilename);	return bRet;}bool WWXMLManager::saveXMLfile(){	if (m_pXMLdocument)	{		if (tinyxml2::XML_SUCCESS != m_pXMLdocument->Savefile(m_strXMLfilePath.c_str()))		{			return false;		}	}	return true;}bool WWXMLManager::getDefaultXMLBoolForKey(const char* pKey){	return CCUserDefault::getInstance()->getBoolForKey(pKey);}bool WWXMLManager::getDefaultXMLBoolForKey(const char* pKey,bool defaultValue){	return CCUserDefault::getInstance()->getBoolForKey(pKey,defaultValue);}int WWXMLManager::getDefaultXMlintegerForKey(const char* pKey){	return CCUserDefault::getInstance()->getIntegerForKey(pKey);}int WWXMLManager::getDefaultXMlintegerForKey(const char* pKey,int defaultValue){	return CCUserDefault::getInstance()->getIntegerForKey(pKey,defaultValue);}float WWXMLManager::getDefaultXMLfloatForKey(const char* pKey){	return CCUserDefault::getInstance()->getfloatForKey(pKey);}float WWXMLManager::getDefaultXMLfloatForKey(const char* pKey,float defaultValue){	return CCUserDefault::getInstance()->getfloatForKey(pKey,defaultValue);}double WWXMLManager::getDefaultXMLDoubleForKey(const char* pKey){	return CCUserDefault::getInstance()->getDoubleForKey(pKey);}double WWXMLManager::getDefaultXMLDoubleForKey(const char* pKey,double defaultValue){	return CCUserDefault::getInstance()->getDoubleForKey(pKey,defaultValue);}std::string WWXMLManager::getDefaultXMLStringForKey(const char* pKey){	return CCUserDefault::getInstance()->getStringForKey(pKey);}std::string WWXMLManager::getDefaultXMLStringForKey(const char* pKey,const std::string & defaultValue){	return CCUserDefault::getInstance()->getStringForKey(pKey,defaultValue);}voID WWXMLManager::setDefaultXMLBoolForKey(const char* pKey,bool value){	CCUserDefault::getInstance()->setBoolForKey(pKey,value);}voID WWXMLManager::setDefaultXMlintegerForKey(const char* pKey,int value){	CCUserDefault::getInstance()->setIntegerForKey(pKey,value);}voID WWXMLManager::setDefaultXMLfloatForKey(const char* pKey,float value){	CCUserDefault::getInstance()->setfloatForKey(pKey,value);}voID WWXMLManager::setDefaultXMLDoubleForKey(const char* pKey,double value){	CCUserDefault::getInstance()->setDoubleForKey(pKey,value);}voID WWXMLManager::setDefaultXMLStringForKey(const char* pKey,const std::string & value){	CCUserDefault::getInstance()->setStringForKey(pKey,value);}voID WWXMLManager::purgeSharedUserDefault(){	CCUserDefault::getInstance()->destroyInstance();}const std::string& WWXMLManager::getDefaultXMLfilePath(){	return CCUserDefault::getInstance()->getXMLfilePath();}bool WWXMLManager::isXmlfileExist(const char* strfilePath){		file *fp = fopen(strfilePath,"r");	bool bRet = false;	if (fp)	{		bRet = true;		fclose(fp);	}	return bRet;}std::string WWXMLManager::getWWStringFromXML(const std::string &strKey,const std::string &strSection){	if (strKey.empty() || strSection.empty())	{		cclOG("key or section is empty!");		return "";	}	//加载XML文件	WWXMLManager myXML;	if (!myXML.loadXMLfile(WWSTRING_XML_PATH.c_str()))	{		cclOG("load string resource error!");		return "";	}	//循环获取节点名为stirng,属性名为name,属性值为strKey的节点	WWXMLNode node = myXML.getXMLRootNode().findChildNode(strSection.c_str());	if (!node.isNulL())	{		WWXMLNode childNode = node.findChildNode("string");		while (!childNode.isNulL())		{			if (strcmp(childNode.firstAttribute("name").value(),strKey.c_str()) == 0)			{				std::string strResult = childNode.getNodeValue();				//win32,模拟器上显示需要转换成gbk编码#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)				GBKToUTF8(strResult,"GBK","UTF-8");#endif				return strResult;			}			childNode = childNode.findSlibingNode("string");		}	}	return "";}
总结

以上是内存溢出为你收集整理的cocos2dx 3.2 读写XML,基于tinyxml2封装的易使用,更灵活的XML接口。中文国际化。全部内容,希望文章能够帮你解决cocos2dx 3.2 读写XML,基于tinyxml2封装的易使用,更灵活的XML接口。中文国际化。所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1027569.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存