/* * Generate tinyxml2::XMLElement for Object through a tinyxml2::XMLDocument */ static tinyxml2::XMLElement* generateElementForObject(const Value& value, tinyxml2::XMLDocument *doc) { // object is String if (value.getType() == Value::Type::STRING) { tinyxml2::XMLElement* node = doc->NewElement("string"); tinyxml2::XMLText* content = doc->NewText(value.asString().c_str()); node->LinkEndChild(content); return node; } // object is integer if (value.getType() == Value::Type::INTEGER) { tinyxml2::XMLElement* node = doc->NewElement("integer"); tinyxml2::XMLText* content = doc->NewText(value.asString().c_str()); node->LinkEndChild(content); return node; } // object is real if (value.getType() == Value::Type::FLOAT || value.getType() == Value::Type::DOUBLE) { tinyxml2::XMLElement* node = doc->NewElement("real"); tinyxml2::XMLText* content = doc->NewText(value.asString().c_str()); node->LinkEndChild(content); return node; } //object is bool if (value.getType() == Value::Type::BOOLEAN) { tinyxml2::XMLElement* node = doc->NewElement(value.asString().c_str()); return node; } // object is Array if (value.getType() == Value::Type::VECTOR) return generateElementForArray(value.asValueVector(), doc); // object is Dictionary if (value.getType() == Value::Type::MAP) return generateElementForDict(value.asValueMap(), doc); CCLOG("This type cannot appear in property list"); return nullptr; }
/* * Use tinyxml2 to write plist files */ bool FileUtils::writeToFile(ValueMap& dict, const std::string &fullPath) { //CCLOG("tinyxml2 Dictionary %d writeToFile %s", dict->_ID, fullPath.c_str()); tinyxml2::XMLDocument *doc = new tinyxml2::XMLDocument(); if (nullptr == doc) return false; tinyxml2::XMLDeclaration *declaration = doc->NewDeclaration("xml version=\"1.0\" encoding=\"UTF-8\""); if (nullptr == declaration) { delete doc; return false; } doc->LinkEndChild(declaration); tinyxml2::XMLElement *docType = doc->NewElement("!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\""); doc->LinkEndChild(docType); tinyxml2::XMLElement *rootEle = doc->NewElement("plist"); rootEle->SetAttribute("version", "1.0"); if (nullptr == rootEle) { delete doc; return false; } doc->LinkEndChild(rootEle); tinyxml2::XMLElement *innerDict = generateElementForDict(dict, doc); if (nullptr == innerDict ) { delete doc; return false; } rootEle->LinkEndChild(innerDict); bool ret = tinyxml2::XML_SUCCESS == doc->SaveFile(fullPath.c_str()); delete doc; return ret; }