zoukankan      html  css  js  c++  java
  • 详解cocos2d-x使用tinyxml2解析&存储xml

    我用的是2.1.4的cocos2d-x,里面自带有tinyxml2库。好像2.1.0以上都有了

    导入头文件:
    #include "support/tinyxml2/tinyxml2.h"  

    一:创建xml并保存

    //要储存XML文件的路径
        std::string filePath = CCFileUtils::sharedFileUtils()->getWritablePath() + "wociao.xml";
        //xml文档
        XMLDocument *pDoc = new XMLDocument();
        if (NULL==pDoc) {
            return ;
        }
        //xml声明
        XMLDeclaration *pDel = pDoc->NewDeclaration("xml version="1.0" encoding="UTF-8"");
        if (NULL==pDel) {
            return ;
        }
        pDoc->LinkEndChild(pDel);
        //节点plist
        XMLElement *plistElement = pDoc->NewElement("plist");
        plistElement->SetAttribute("version", "1.0");//给节点设置属性
        pDoc->LinkEndChild(plistElement);
            //节点dict
            XMLElement *dictElement = pDoc->NewElement("dict");
            plistElement->LinkEndChild(dictElement);
                //节点key
                XMLElement *keyElement = pDoc->NewElement("key");
                keyElement->LinkEndChild(pDoc->NewText("keyText"));//给节点设置值
                dictElement->LinkEndChild(keyElement);
                //节点string
                XMLElement *stringElement = pDoc->NewElement("string");
                stringElement->LinkEndChild(pDoc->NewText("stringText"));//给节点设置值
                dictElement->LinkEndChild(stringElement);
                //节点array
                XMLElement *arrayElemet = pDoc->NewElement("array");
                dictElement->LinkEndChild(arrayElemet);
                    for (int i = 0; i<3; i++) {
                        XMLElement *strEle = pDoc->NewElement("string");
                        strEle->LinkEndChild(pDoc->NewText("icon"));
                        arrayElemet->LinkEndChild(strEle);
                    }
        
        pDoc->SaveFile(filePath.c_str());//保存文件 参数:路径
        pDoc->Print();//打印
        delete pDoc;

    打印的结果

    <?xml version="1.0" encoding="UTF-8"?>
    <plist version="1.0">
        <dict>
            <key>keyText</key>
            <string>stringText</string>
            <array>
                <string>icon</string>
                <string>icon</string>
                <string>icon</string>
            </array>
        </dict>
    </plist>

    二:解析xml 

    //xml文件路径
        std::string filePath = CCFileUtils::sharedFileUtils()->getWritablePath() + "wociao.xml";
    //xmlDoc
        XMLDocument *pDoc = new XMLDocument();
        pDoc->LoadFile(filePath.c_str());
        //得到跟节点
        XMLElement *rootEle = pDoc->RootElement();
        //打印节点的值
        CCLog("%s",rootEle->GetText());
        //节点的第一个属性
        const XMLAttribute *attribute = rootEle->FirstAttribute();
        //打印属性的名字和值
        CCLog("%s %s",attribute->Name(),attribute->Value());    
        //查找节点的属性值
        float value = 0.1f;
        rootEle->QueryFloatAttribute("version", &value);
        CCLog("%f",value);
        //设置节点属性值
        rootEle->SetAttribute("version", 1.4);
        //跟节点的第一个字节点 dict
        XMLElement *dictEle = rootEle->FirstChildElement();
        //dict下面的子节点 key
        XMLElement *keyEle = dictEle->FirstChildElement();
        //打印key节点的值
        CCLog("%s,%s",keyEle->Name(),keyEle->GetText());
        //key节点的next节点 string
        XMLElement *stringEle = keyEle->NextSiblingElement();
        CCLog("%s,%s",stringEle->Name(),stringEle->GetText());
        //string节点的子节点
        XMLElement *nulXmlEle = stringEle->FirstChildElement();
        if (NULL == nulXmlEle) {
            CCLog("string下面没有子点了");
        }
        //保存xml
        pDoc->SaveFile(filePath.c_str());
  • 相关阅读:
    怎么使用 Jupyter Notebook 进入指定的目录
    安卓手机安装Xposed框架/钉钉虚拟定位
    Some of the Example google dorks 2019谷歌hacker语法检索表
    在心脏内部,普通的WEB用户会怎样? 心脏滴血漏洞分析学习
    Windows的安全配置基础,打造一个安全点的Windows
    计算机存储单位
    Python如何安装whl文件,python安装py包
    jdk文件中javaw的作用
    msfconsole基础使用,简洁高效学习版
    VirtualBox报错:不能为虚拟电脑XXX打开一个新任务
  • 原文地址:https://www.cnblogs.com/junzitandandan/p/3246758.html
Copyright © 2011-2022 走看看