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);


}

 

转载于:https://www.cnblogs.com/gamesky/p/4281423.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值