Cocos2d-x xml解析

來源:互聯網
上載者:User

標籤:

Cocos2d-x xml解析

Cocos2d-x 已經加入了tinyxml2用於xml的解析。3.0版本位於external/tinyxml2下。2.x版本位於cocos2dx/support/tinyxml2下。

tinyxml2 Github地址:https://github.com/leethomason/tinyxml2

協助文檔地址:http://grinninglizard.com/tinyxml2docs/index.html

產生xml文檔
  1. 引入標頭檔

    #include "tinyxml2/tinyxml2.h"using namespace tinyxml2;
  2. xml文檔產生

    void  HelloWorld::makeXML(const char *fileName){std::string filePath = FileUtils::getInstance()->getWritablePath() + fileName;XMLDocument *pDoc = new 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;}
  3. 列印結果

    <?xml version="1.0" encoding="UTF-8"?><plist version="1.0"><!--this is xml comment--><dic>    <key>Text</key>    <array>        <name>Cocos2d-x</name>        <name>Cocos2d-x</name>        <name>Cocos2d-x</name>    </array></dic></plist>

上面代碼使用tinyxml簡單產生了一個xml文檔。

解析xml

下面我們就來解析上面建立的xml文檔

  1. 引入標頭檔

    #include "tinyxml2/tinyxml2.h"using namespace tinyxml2;
  2. xml解析

    void HelloWorld::parseXML(const char *fileName){std::string filePath = FileUtils::getInstance()->getWritablePath() + fileName;XMLDocument *pDoc = new XMLDocument();XMLError errorId = pDoc->LoadFile(filePath.c_str());if (errorId != 0) {    //xml格式錯誤    return;}XMLElement *rootEle = pDoc->RootElement();//擷取第一個節點屬性const XMLAttribute *attribute = rootEle->FirstAttribute();//列印節點屬性名稱和值log("attribute_name = %s,attribute_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;}

    在節點解析過程中,注意對擷取到的節點進行判空處理。

  3. 解析結果列印

    cocos2d: attribute_name = version,attribute_value = 1.0cocos2d: keyEle Text= Textcocos2d: childEle Text= Cocos2d-xcocos2d: childEle Text= Cocos2d-xcocos2d: childEle Text= Cocos2d-x
小結

上面的簡單樣本,示範了如何使用tinyxml進行xml文檔產生和解析。更多詳細的協助請參考 tinyxml協助文檔http://grinninglizard.com/tinyxml2docs/index.html

Cocos2d-x xml解析

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.