How to load xml file in cocos2d-x

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

Use fread or libxml interfaces directly.

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

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

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

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

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

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!

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
{

}`