zoukankan      html  css  js  c++  java
  • cocos2dx3.4 导出节点树到XML文件

    l利用cocostudio做UI和场景时,经常要去获取某个节点,cocostudio2.1开始加入了文件的概念,可以创建场景,节点,层等文件,把公用的东西创建到文件里,然后把这个文件拖到场景里使用,达到充分利用的目的。

    从场景中获取某个文件的节点时,经常发现找不到,于是把场景节点树导出为XML文件,节点树也一目了然了,节点属性如名称等也可以根据需要导出来,效果如下:

    代码:

        //导出从pNode节点开始的节点树到XML文件,bIsRoot:是否为根节点
        static void ExptSceneTreeToXml(Node *pNode,bool bIsRoot=false,tinyxml2::XMLElement *pParentEle=nullptr);
    void UiManage::ExptSceneTreeToXml(Node *pNode,bool bIsRoot,tinyxml2::XMLElement *pParentEle)
    {
    
        Node *pChild=nullptr;
        static tinyxml2::XMLDocument *pDoc=nullptr;
        string tempStr="";
        do 
        {
            if (pNode)
            {
                //创建XML
                if (bIsRoot)
                {
                    pDoc=new tinyxml2::XMLDocument();
                    CC_BREAK_IF(pDoc==nullptr);
                    //xml申明
                    tinyxml2::XMLDeclaration *pDecl= pDoc->NewDeclaration("version="1.0" encoding="UTF-8"");
                    CC_BREAK_IF(pDecl==nullptr);
                    pDoc->LinkEndChild(pDecl);
                    //根节点
                    tinyxml2::XMLElement *pRootEle=pDoc->NewElement("RootNode");
                    tempStr=pNode->getName();
                    pRootEle->SetAttribute("Name",tempStr.c_str());
    
                    pDoc->LinkEndChild(pRootEle);
                    pParentEle=pRootEle;
    
                }
                //
                //遍历子节点
                if (pNode->getChildrenCount()>0)
                {
                    //节点1
                    tinyxml2::XMLElement *pChildREle0=pDoc->NewElement("Children");
                    pParentEle->LinkEndChild(pChildREle0);
                    //
                    const Vector<Node*>& childVec=pNode->getChildren();
                    for (auto chid:childVec)
                    {
                        if (chid)
                        {
                            //节点2
                            tinyxml2::XMLElement *pChildEle=pDoc->NewElement("Node");
                            tempStr=chid->getName();
                            pChildEle->SetAttribute("Name",tempStr.c_str());
                            pChildREle0->LinkEndChild(pChildEle);
                            if (chid->getChildrenCount()>0)
                            {
                                ExptSceneTreeToXml(chid,false,pChildEle);
                            }
                        }
                    }
                    if (bIsRoot)
                    {
                        string xmlPath=GetResPath()+"expt\"+pNode->getName()+"SceneTree.xml";
                        CCLOG("-----save xml name=%s",xmlPath.c_str());
                        pDoc->SaveFile(xmlPath.c_str());
                    }
                }
    
    
    
            }
    
        } while (0);
    
    
    }
  • 相关阅读:
    hdu 5007 水题 (2014西安网赛A题)
    hdu 1698 线段树(成段替换 区间求和)
    poj 3468 线段树 成段增减 区间求和
    hdu 2795 公告板 (单点最值)
    UVaLive 6833 Miscalculation (表达式计算)
    UVaLive 6832 Bit String Reordering (模拟)
    CodeForces 124C Prime Permutation (数论+贪心)
    SPOJ BALNUM (数位DP)
    CodeForces 628D Magic Numbers (数位DP)
    POJ 3252 Round Numbers (数位DP)
  • 原文地址:https://www.cnblogs.com/gamesky/p/4281423.html
Copyright © 2011-2022 走看看