Cocos2d解析XML

前端之家收集整理的这篇文章主要介绍了Cocos2d解析XML前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

生成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文档

    #include "tinyxml2/tinyxml2.h" using namespace tinyxml2;
  1. 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;
    
    }

    在节点解析过程中,注意对获取到的节点进行判空处理。

  2. 解析结果打印

    cocos2d: attribute_name = version,attribute_value = 1.0
    cocos2d: keyEle Text= Text
    cocos2d: childEle Text= Cocos2d-x
    cocos2d: childEle Text= Cocos2d-x
    cocos2d: childEle Text= Cocos2d-x
原文链接:https://www.f2er.com/cocos2dx/340983.html

猜你在找的Cocos2d-x相关文章