转载请保留原地址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. android在布局中动态增加view时的层级控制
  2. 升级代码的大概设计
  3. OpenCore 的代码结构
  4. Android(安卓)APIDemos 研读之一:android.graphics.Movie
  5. 从Android项目学习Kotlin(一)
  6. Android、JUnit深入浅出(一)——JUnit初步解析
  7. Android(安卓)7 新特性浅析
  8. 图解Android源代码下载指南
  9. cocos2dx在windows下开发,编译到android上

随机推荐

  1. Ceph CSI 3.1发布
  2. Mybatis plus 整合springboot 出现的Inva
  3. jQuery:常用dom和事件操作,jQuery中的$.a
  4. 谁要是敢用Map传参数,我喵喵就打死他
  5. UNI使用蓝牙连接设备传输数据
  6. 怎么将swagger API导出为HTML或者PDF
  7. Vue的v-bind,v-on, v-model,v-if,v-for,
  8. jQuery常用操作; jQuery中的$.ajax方法; Vu
  9. $()的四种类型; jQuery方法;jQuery对象转js
  10. 智慧平安社区综合管理平台建设,社区网格化