简体   繁体   中英

Can't load and parse xml file on android in c++

I'm working on a Cocos2dx C++ project and I am trying to load a XMLfile and parse it with rapidxml.

The Win32 build works great with this code.

rapidxml::file<> xmlFile("myxml.xml"); 
xml_document<> doc; // character type defaults to char
doc.parse<0>(xmlFile.data());    // 0 means default parse flags
xml_node<> *main = doc.first_node();  //Get the main node that contains everything

This does not work for the Android build. It's most likely because "myxml.xml" can't be found. So my solution was to load the file in a different way.

So I tried to replace the xmlFile.data() with this. The mobile could now locate and read the data but I was unable to parse it trough rapidxml.

const unsigned char* xmldata = CCFileUtils::sharedFileUtils()->getFileData("myxml.xml", "r", &tmpSize);

But then I have no idea how to parse it.

This is another way of getting the data but still no idea how to parse with rapidxml.

std::string xmldata2 = CCFileUtils::sharedFileUtils()->getStringFromFile("myxml.xml");

Any ideas how to parse this in rapidxml, or any other suggestions how to solve this?

I'm new to c++ so any help is very much appreciated.

I have used PugiXML and it is relatively simple to use. A file is read as:

xml_document* doc = new xml_document;
unsigned char* pBuffer = NULL;
unsigned long bufferSize = 0;

std::string fullPath = CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(filename);

pBuffer = CCFileUtils::sharedFileUtils()->getFileData(fullPath.c_str(), "r", &bufferSize);

if(bufferSize == 0)
{
    CCLog("file failed to open");
    delete doc;
}

xml_parse_result results = doc->load_buffer(pBuffer, bufferSize);
CCLog("filename: %s | result: %s | status: %d",filename, results.description(), results.status);

if(pBuffer)
{
    delete [] pBuffer;
    pBuffer = NULL;
}
xml_node mainNode = doc->child("main");

mainNode then has contents of the parsed file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM