首页 > 代码库 > cocos2d-x3.0 XML解析

cocos2d-x3.0 XML解析

在2dx3.0中xml解析已经不用自己找库了,已经为我们集成好了。

text.xml

<?xml version ="1.0" encoding ="UTF8" standalone="yes" ?>
<?xml-stylesheet type="text/xsl" href=http://www.mamicode.com/"yxfqust.xsl" ?>>
添加头文件:.h

#include "cocos-ext.h"
#include "tinyxml2/tinyxml2.h"
using namespace tinyxml2;

.cpp

 //获取文件路径
        const char* file_path = FileUtils::getInstance()->fullPathForFilename("text.xml").c_str();
       log("external file path = %s",file_path);

        
        XMLDocument* myDocment = new tinyxml2::XMLDocument();
        //加载文件
        myDocment->LoadFile(file_path);
        
        tinyxml2::XMLElement* rootElement = myDocment->RootElement();
      
        
        XMLElement* studentElement = rootElement->FirstChildElement();

        while (studentElement) {
          
            
            XMLElement* element = studentElement->FirstChildElement();
            while (element) {
                 log("信息 : %s",element->GetText());
                element = element->NextSiblingElement();
                
            }
            studentElement = studentElement->NextSiblingElement();
            
        }