Saturday, September 6, 2014

cocos2d-x 3.0 parse xml used tinyxml

1.bellows is cocos2d-x 3.0 parse xml from file code,because i writed some error parse code,and fixed it.

bool RuntimeProfile::init(){
reset();

std::string currentLanguageCode = Application::getInstance()->getCurrentLanguageCode();
if (currentLanguageCode == "zh")
currentLanguageCode = "values-zh-rCN";

unsigned char* buf = 0;
do{

if (currentLanguageCode.empty())
break;

try{
std::string file_path = FileUtils::getInstance()->fullPathForFilename("locales/" + currentLanguageCode + "/strings.xml");
if (!FileUtils::getInstance()->isFileExist(file_path))
break;

ssize_t size;
buf = FileUtils::getInstance()->getFileData(file_path, "r", &size);
if (!buf)
break;

tinyxml2::XMLDocument* myDocment = new tinyxml2::XMLDocument();

if (tinyxml2::XML_NO_ERROR != myDocment->Parse((const char*)buf, size)){
break;
}

tinyxml2::XMLElement* rootElement = myDocment->RootElement();
if (!rootElement)
break;

tinyxml2::XMLElement* childElement = rootElement->FirstChildElement("string");

const char* key = NULL;

while (childElement) {
key = childElement->Attribute("name");
if (key && key[0])
mLangDictory[key] = childElement->GetText();

childElement = childElement->NextSiblingElement("string");
}
}
catch (...){
}

} while (false);

CC_SAFE_FREE(buf);

return true;
}

No comments: