Forum > C++ > How to load xml file in cocos2d-x

How to load xml file in cocos2d-x

By Gurusagar Posted 2011-03-11 21:09 Comments 8
Gurusagar
  • Posts: 18

Hi ,

iam trying to load xml file in using CCfileutils but it is not loading , is there any other way to load it .

Thanks,
Gurusagar

walzer
  • Posts: 1735
  • Location: Xiamen, China

#1 RE: 2011-03-11 21:47

Use fread or libxml interfaces directly.

Less is More. Keep Focus.

Gurusagar
  • Posts: 18

#2 RE: 2011-03-11 22:40

Hi Walzer Wang,

Iam getting problem with the path where xml is located ,

string file = “tut.xml”;
string file =CCFileUtils::fullPathFromRelativePath(“Resource-iPhone/Data/tut.xml”); /// it is not loading
TiXmlDocument doc(file.c_str());

Thanks,
Gurusagar

walzer
  • Posts: 1735
  • Location: Xiamen, China

#3 RE: 2011-03-11 23:07

You code looks correctly, but where do you locate the txt.xml file in the android apk?
You can ref to the sources in

bool CCTMXMapInfo::initWithTMXFile(const char *tmxFile)

in cocos2dx/tileMap_parallax_nodes/CCTMXXMLParser.cpp
And trace from CCTMXTiledMap::tiledMapWithTMXFile(…) in tests/TileMapTest/TileMapTest.cpp. The TMX file is xml format indeed

Less is More. Keep Focus.

Gurusagar
  • Posts: 18

#4 RE: 2011-03-14 07:57

Hi ,

from this code CCFileUtils::fullPathFromRelativePath(“tut.xml”);
Iam to load the xml file in the iphone form Resources-iphone , where as in android it is not loading i located that particular xml in the assets folder ,

Thanks,
Gurusagar

walzer
  • Posts: 1735
  • Location: Xiamen, China

#5 RE: 2011-03-15 07:54

Sorry, I see the reply 24 hours after your post. Busy releasing 0.8.0 today.
Well, I think it’s better to write a document explaining how CCFiletils::fullPathFromRelativePath works on all platforms. An issue #397 is created and assign to Bin, the author of CCFileUtils

Less is More. Keep Focus.

Gurusagar
  • Posts: 18

#6 RE: 2011-03-15 22:53

Hi Walzer Wang ,
Thanks for providing Multi-touch & Accelerometer support on android in new version .I am using tiny xml to load the xml document , does cocos2d-x support tinyxml library . when i use this library it is working in Iphone . can you please give information about loading xml files in android and CCFileUtils

Thanks,
Gurusagar

zhangbin
  • Posts: 117
  • Location: Xiamen, China

#7 RE: 2011-03-16 02:17

Guru sagar wrote:

Hi Walzer Wang ,
Thanks for providing Multi-touch & Accelerometer support on android in new version .I am using tiny xml to load the xml document , does cocos2d-x support tinyxml library . when i use this library it is working in Iphone . can you please give information about loading xml files in android and CCFileUtils
>
>
Thanks,
Gurusagar

  1. cocos2d-x use libxml library now,so we don’t plan to support tinyxml.
  2. If you want to load particular xml files in the assets folder.Your code should be like this:

    std::string fullPath = CCFileUtils::fullPathFromRelativePath("tut.xml");
    unsigned char* pBuffer = NULL;
    unsigned long bufferSize = 0;
    pBuffer = CCFileUtils::getFileData(fullPath.c_str(), "r", &bufferSize);

    // Now the xml file data have be in the buffer,use it
    ...

    // At last,the buffer should be deleted
    if (pBuffer)
    {
    delete [] pBuffer;
    pBuffer = NULL;
    }

Hope it will help you!

feiting_schatt
  • Posts: 11

#8 RE: 2012-05-09 16:50

This is just a full example, since the usual TinyXML functions cause memory leaks since doc.Load doesn’t work on Android and if you bypass load and use ParseDeep (which I posted somewhere), it doesn’t reset variables properly.

Here’s a full loading example using Cocos2d’s managed model

`newPath = CCFileUtils::fullPathFromRelativePath(relativePagePath);

CCFileData data(newPath.c_str(), "rt"); //taken from BMFont - decompresses file from Android
char* pBuffer = (char*) data.getBuffer();

XMLDocument doc;
int result = XML_SUCCESS;

result = doc.Parse(pBuffer);
if( result == doc.ErrorID() ) //if success, then parse
{

}`

Loggin to reply

Copyright © 2010 - 2013 Cocos2d-x.orgClustrmaps