http://blog.csdn.net/linyongliang
欢迎转载:请保留原文出处 谢谢
本次介绍如何在cocos2d-x上使用tinyXml库 对于tinyxml我不做多介绍,不懂朋友度娘 谷哥
tinyxml 在win32上是可以直接使用没啥问题。但是放到android上会出现路径找不到,这里就是要帮大家解决这个问题。其实这个代码是人家写的 我改了一点让2.X可 以使用。谢谢那位朋友 。具体出处忘了 真不好意思
其中引擎自带了一个解析器。里面是有文件路径获取方法的。那位朋友也是根据这个原来来修改tinyXml库
具体我吧代码弄上来,其实是给tinyXml加一个加载的成员函数 如下
bool TiXmlDocument::LoadFileByCocos2d( const char * filename, TiXmlEncoding encoding /*= TIXML_ENCODING_UTF8*/ )
{
if ( !filename )
{
SetError( TIXML_ERROR_OPENING_FILE, 0, 0, TIXML_ENCODING_UNKNOWN );
return false;
}
// Delete the existing data:
Clear();
location.Clear();
// Get the file size, so we can pre-allocate the string. HUGE speed impact.
const char *pfilePath = CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(filename);
TIXML_STRING __filename( pfilePath );
value = __filename;
unsigned long length = 0;
char *pBuffer =(char*) CCFileUtils::sharedFileUtils()->getFileData(pfilePath, "rt", &length);
if (!pBuffer)
{
SetError( TIXML_ERROR_OPENING_FILE, 0, 0, TIXML_ENCODING_UNKNOWN );
return false;
}
// Strange case, but good to handle up front.
if ( length <= 0 )
{
SetError( TIXML_ERROR_DOCUMENT_EMPTY, 0, 0, TIXML_ENCODING_UNKNOWN );
return false;
}
const char* p = pBuffer; // the read head
char* q = pBuffer; // the write head
const char CR = 0x0d;
const char LF = 0x0a;
pBuffer[length] = 0;
while( *p ) {
assert( p < (pBuffer+length) );
assert( q <= (pBuffer+length) );
assert( q <= p );
if ( *p == CR ) {
*q++ = LF;
p++;
if ( *p == LF ) { // check for CR+LF (and skip LF)
p++;
}
}
else {
*q++ = *p++;
}
}
assert( q <= (pBuffer+length) );
*q = 0;
Parse( pBuffer, 0, encoding );
return !Error();
}
bool TiXmlDocument::SaveFile( const char * filename ) const
{
// The old c stuff lives on...
FILE* fp = TiXmlFOpen( filename, "w" );
if ( fp )
{
bool result = SaveFile( fp );
fclose( fp );
return result;
}
return false;
}//具体哪里加 自己找
bool loadConfig()//如何使用 简单个例子 自己看吧
{
bool ret=false;
do {
TiXmlDocument doc;
TiXmlElement *elementRoot,*elementIndex,*elementLast,*elementChildIndex,*elementChildLast;
TiXmlAttribute *attribute;
int id;
doc.LoadFileByCocos2d("config.xml");
elementRoot=doc.FirstChildElement();
attribute=elementIndex->FirstAttribute();
id=attribute->IntValue();
vector<Point>vecPoint;
elementChildIndex=elementIndex->FirstChildElement("vertex");
elementChildLast=(TiXmlElement *)elementIndex->LastChild("vertex");
while(elementChildIndex!=elementChildLast)
{
attribute=elementChildIndex->FirstAttribute();
point.x=attribute->IntValue();
attribute=attribute->Next();
point.y=attribute->IntValue();
vecPoint.push_back(point);
elementChildIndex=elementChildIndex->NextSiblingElement();
}
attribute=elementChildIndex->FirstAttribute();
point.y=attribute->IntValue();
attribute=attribute->Next();
point.x=attribute->IntValue();
vecPoint.push_back(point);
mapArray.insert(map<int_16_u,vector<Point> >::value_type(id,vecPoint));
doc.Clear();
ret = true;
} while (0);
return ret;
}
android的Android.mk配置
LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := game_shared LOCAL_MODULE_FILENAME := libgame LOCAL_SRC_FILES := hellocpp/main.cpp \ ../../Classes/AppDelegate.cpp \ ../../Classes/SceneGreet.cpp \ ../../Classes/SceneSet.cpp \ ../../Classes/ScenePlay.cpp \ ../../Classes/Bullet.cpp \ ../../Classes/Fish.cpp \ ../../Classes/tinyxml.cpp \ ../../Classes/tinystr.cpp \ ../../Classes/tinyxmlparser.cpp \ ../../Classes/tinyxmlerror.cpp \ ../../Classes/Config.cpp \ LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../Classes LOCAL_WHOLE_STATIC_LIBRARIES := cocos2dx_static cocosdenshion_static cocos_extension_static include $(BUILD_SHARED_LIBRARY) $(call import-module,CocosDenshion/android) \ $(call import-module,cocos2dx) \ $(call import-module,extensions)