ReZero's Utopia.

ToPlistFile

Word count: 218Reading time: 1 min
2016/11/10 Share

如何生成plist,xml文件

void TowerPosEditorLayer::outputPosToPlistFile(){
    String* sTowerPosPath = String::createWithFormat("tollgate/towerPos_level_%d.plist", m_iCurLevel);
    outputPosToPilistFile(m_towerPosList, sTowerPosPath->getCString());
}

void TowerPosEditorLayer::outputPosToPilistFile(Array* posList, const char* sFilePath){
    FILE* file = fopen(sFilePath, "w");

    fprintf(file, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
    fprintf(file, "<!DOCTYPE plist PUBLIC\"-//Apple//DTD PLIST 1.0//EN\"\"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n");

    fprintf(file, "<plist version=\"1.0\">\n");
    fprintf(file, "<array>\n");

    Object* obj = NULL;
    PosBase* posBase = NULL;
    CCARRAY_FOREACH(posList, obj){
        posBase = dynamic_cast<PosBase*>(obj);

        if (posBase != NULL){
            fprintf(file, "    <dict>\n");

            fprintf(file, "    <key>x</key>\n");
            fprintf(file, "    <integer>%.0f</integer>\n", posBase->getPos().x);

            fprintf(file, "    <key>y</key>\n");
            fprintf(file, "    <integer>%.0f</integer>\n", posBase->getPos().y);

            fprintf(file, "    </dict>\n");

        }
    }

    fprintf(file, "</array>\n");
    fprintf(file, "</plist>\n");

    fclose(file);

}
CATALOG