转载请保留原地址http://www.cocos2dres.com/post/88.html谢谢!!!


下载TinyXml库,集成到Cococs2d-x工程中,编译运行,写测试代码,一切顺利!

可是CygWin编译,生成APK后,在Android真机上跑,程序崩溃!这可愁死人了!!!!!

于是,周末有空看了下Cocos2dx的源码.
发现Android下的文件都是从zip里面读取,所以给Tinyxml增加了一个方法 可以使它在android下正确读取xml文件.


[html]view plaincopy

  1. ///LoadXmlformmemorybuff.Returnstrueifsuccessful.

  2. boolTiXmlDocument::LoadMemory(constchar*pBuff,intlength,TiXmlEncodingencoding)

  3. {

  4. if(!pBuff||length==0)

  5. {

  6. SetError(TIXML_ERROR,0,0,TIXML_ENCODING_UNKNOWN);

  7. returnfalse;

  8. }

  9. //Ifwehaveafile,assumeitisallonebigXMLfile,andreaditin.

  10. //Thedocumentparsermaydecidethedocumentendssoonerthantheentirefile,however.

  11. TIXML_STRINGdata;

  12. data.reserve(length);

  13. char*buf=newchar[length+1];

  14. buf[0]=0;

  15. memcpy(buf,pBuff,length);

  16. constchar*lastPos=buf;

  17. constchar*p=buf;

  18. buf[length]=0;

  19. while(*p){

  20. assert(p<(buf+length));

  21. if(*p==0xa){

  22. //Newlinecharacter.Nospecialrulesforthis.Appendallthecharacters

  23. //sincethelaststring,andincludethenewline.

  24. data.append(lastPos,(p-lastPos+1));//append,includethenewline

  25. ++p;//movepastthenewline

  26. lastPos=p;//andpointtothenewbuffer(maybe0)

  27. assert(p<=(buf+length));

  28. }

  29. elseif(*p==0xd){

  30. //Carriagereturn.Appendwhatwehavesofar,then

  31. //handlemovingforwardinthebuffer.

  32. if((p-lastPos)>0){

  33. data.append(lastPos,p-lastPos);//donotaddtheCR

  34. }

  35. data+=(char)0xa;//apropernewline

  36. if(*(p+1)==0xa){

  37. //Carriagereturn-newlinesequence

  38. p+=2;

  39. lastPos=p;

  40. assert(p<=(buf+length));

  41. }

  42. else{

  43. //itwasfollowedbysomethingelse...thatispresumablycharactersagain.

  44. ++p;

  45. lastPos=p;

  46. assert(p<=(buf+length));

  47. }

  48. }

  49. else{

  50. ++p;

  51. }

  52. }

  53. //Handleanyleftovercharacters.

  54. if(p-lastPos){

  55. data.append(lastPos,p-lastPos);

  56. }

  57. delete[]buf;

  58. buf=0;

  59. Parse(data.c_str(),0,encoding);

  60. if(Error())

  61. {

  62. returnfalse;

  63. }

  64. returntrue;

  65. }


传入一个buff,然后交给TineXml解析。

使用示例:

TiXmlDocument* pXMLDoc = new TiXmlDocument( szSchemeName );

unsigned long nLength = 0;
char* pBuff = (char*)cocos2d::CCFileUtils::sharedFileUtils()->getFileData(cocos2d::CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(szSchemeName),"rt", &nLength );

pXMLDoc->LoadMemory( pBuff, nLength );

SAFE_DELARR( pBuff );

.... 你的代码在这里

delete pXMLDoc;


更多相关文章

  1. Unexpected namespace prefix "xmlns" found for tag LinearLayo
  2. OpenCore 的代码结构
  3. Android创建文件夹
  4. 图解Android源代码下载指南
  5. android 数据库技术 | 文件系统,SDCARD访问,CONTENT PROVIDER,SQLIT
  6. Android studio 使用心得(六)---android studio 如何加载.so文件
  7. 编写Android中直接可运行的二进制文件

随机推荐

  1. Android 8.0(Android O) AccountManager 行
  2. android之异步任务
  3. android 简单利用百度地图api
  4. Android学习手记(4) BroadcastReceiver监
  5. 深入理解Android日志系统
  6. Android Activity和Intent机制
  7. (2019年10月更新) Android 最全的底部导
  8. Android炫酷的3D球型标签云开源库 3dTagC
  9. Android(安卓)Switch控件介绍
  10. 安卓 打开app进入第一个指定的Activity