cocos2d-x 读写 xml 文件

时间:2023-03-09 05:22:34
cocos2d-x 读写 xml 文件

cocos2d-x 读写 xml 文件

A product of cheungmine

使用cocos2d-x开发2d游戏确实方便,但是对于一般的小游戏,经常需要的工作是UI布局设计和调整,代码改来改去,真不方便。现成的Cocos Studio或者SpriteBuilder当然更适合设计游戏。作为程序员,如果想在纯命令行模式下开发游戏,早晚要有自己的UI模块。不妨称之为cocos2d-layout。cocos2d-layout相当于舞台的布景。布景师根据导演的要求(xml)来生成舞台。布景师在现实生活中当然是人来做,在程序里就是一段程序或代码库。这个没用通用的万能的库可以做这个事情,因为游戏的内容是差别很大的。因此一个好的游戏开发师的工具箱里肯定有这样一个瑞士军刀。敝人刚入门,就从最简单的代码开始。

使用xml来做这样的配置是最适合不过的。cocos2d-x提供了读写xml的C++库tinyxml2(https://github.com/leethomason/tinyxml2),当然是开源的,使用起来相当简单:

#include "tinyxml2/tinyxml2.h"

using namespace tinyxml2;

在你的cocos2d代码中放入下面的代码,调用之:

static void makeXml(const char *fileName)
{
    std::string filePath = FileUtils::getInstance()->getWritablePath() + fileName;

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

    //xml 声明(参数可选)
    XMLDeclaration *pDel = pDoc->NewDeclaration("xml version=\"1.0\" encoding=\"UTF-8\"");
    pDoc->LinkEndChild(pDel);
    //添加plist节点
    XMLElement *plistElement = pDoc->NewElement("plist");
    plistElement->SetAttribute("version", "1.0");
    pDoc->LinkEndChild(plistElement);
    XMLComment *commentElement = pDoc->NewComment("this is xml comment");
    plistElement->LinkEndChild(commentElement);
    //添加dic节点
    XMLElement *dicElement = pDoc->NewElement("dic");
    plistElement->LinkEndChild(dicElement);
    //添加key节点
    XMLElement *keyElement = pDoc->NewElement("key");
    keyElement->LinkEndChild(pDoc->NewText("Text"));
    dicElement->LinkEndChild(keyElement);
    XMLElement *arrayElement = pDoc->NewElement("array");
    dicElement->LinkEndChild(arrayElement);
    for (int i = 0; i<3; i++) {
        XMLElement *elm = pDoc->NewElement("name");
        elm->LinkEndChild(pDoc->NewText("Cocos2d-x"));
        arrayElement->LinkEndChild(elm);
    }
    pDoc->SaveFile(filePath.c_str());
    pDoc->Print();
    delete pDoc;
}

static void parseXML(const char *fileName)
{
    std::string filePath = FileUtils::getInstance()->getWritablePath() + fileName;
    tinyxml2::XMLDocument *pDoc = new tinyxml2::XMLDocument();
    XMLError errorId = pDoc->LoadFile(filePath.c_str());
    if (errorId != 0) {
        //xml格式错误
        return;
    }
    XMLElement *rootEle = pDoc->RootElement();
    //获取第一个节点属性
    const XMLAttribute *attribute = rootEle->FirstAttribute();
    //打印节点属性名和值
    log("attribute<em>name = %s,attribute</em>value = %s", attribute->Name(), attribute->Value());
    XMLElement *dicEle = rootEle->FirstChildElement("dic");
    XMLElement *keyEle = dicEle->FirstChildElement("key");
    if (keyEle) {
        log("keyEle Text= %s", keyEle->GetText());
    }
    XMLElement *arrayEle = keyEle->NextSiblingElement();
    XMLElement *childEle = arrayEle->FirstChildElement();
    while ( childEle ) {
        log("childEle Text= %s", childEle->GetText());
        childEle = childEle->NextSiblingElement();
    }
    delete pDoc;
}

当然上面的代码是网上搜来的。只是一个例子。如果想用C来解析xml,有很多选择:libxml,expat, ezxml等。我发现一个很优秀的库:mini-xml。http://www.msweet.org/projects.php?Z3

Mini-XML is a small XML library that you can use to read and write XML
and XML-like data files in your application without requiring large
non-standard libraries. 

Mini-XML only requires an ANSI C compatible compiler (GCC works, as do
 most vendors' ANSI C compilers) and a 'make' program.

Mini-XML supports reading of UTF-8 and UTF-16 and writing of UTF-8
encoded XML files and strings. Data is stored in a linked-list tree
structure, preserving the XML data hierarchy, and arbitrary element
 names, attributes, and attribute values are supported with no
 preset limits, just available memory.

那么我准备用这个来做cocos2d-layout。