在C++中使用TinyXML2解析xml

來源:互聯網
上載者:User

tinyxm2變了很多,文檔又少,坑爹

#include "template.h"#define cp(str) !strcmp(str, argv[i])void make(AnimationInfo & info){    // insert code here...    int error = doc.LoadFile( "Animation.ccb");    if(error){        return;    }    tinyxml2::XMLElement * root = doc.RootElement();    tinyxml2::XMLNode * dict = root->FirstChildElement("dict");    tinyxml2::XMLElement * element = dict->FirstChildElement();    //root    while(element){        std::string nodeGraphStr = "nodeGraph";        std::string seqStr = "sequences";                if(element->GetText() && nodeGraphStr == element->GetText()){            addAnimation(element, &info);        }else if(element->GetText() && seqStr == element->GetText()){            changeDuration(element->NextSiblingElement(), info.duration);        }                element = element->NextSiblingElement();    }    //    doc.SaveFile(info.output.c_str());}int main(int argc, const char * argv[]){    if(argc < 13){        printf("13 arg needed! %d specified!\n", argc);        return -1;    }        //    AnimationInfo info;    bool flag[6];    for(int i=0; i<6; ++i)flag[i]=false;        for(int i=1; i<argc; i+=2){        if(cp("-o")){            assert(!flag[0]);            info.output = argv[i+1];            assert(info.output != "Animation.ccb");            flag[0] = true;        }else if(cp("-d")){            assert(!flag[1]);            info.duration = atof(argv[i+1]);            flag[1] = true;        }else if(cp("-i")){            assert(!flag[2]);            info.interval = atof(argv[i+1]);            flag[2] = true;        }else if(cp("-plist")){            assert(!flag[3]);            info.plistfile = argv[i+1];            flag[3] = true;        }else if(cp("-prefix")){            assert(!flag[4]);            info.filePrefix = argv[i+1];            flag[4] = true;        }else if(cp("-frame")){            assert(!flag[5]);            info.frameCount = atoi(argv[i+1]);            flag[5] = true;        }else {            printf("error arg \"%s\"! \n", argv[i]);        }    }    info.duration = info.interval*info.frameCount;    make(info);    return 0;}

#ifndef AnimationMake_template_h#define AnimationMake_template_h#include <iostream>#include <string>#include <vector>#include "tinyxml2.h"struct AnimationInfo{    float interval; //間隔    std::string pngfile;    std::string plistfile;    int frameCount;    float duration;    std::string filePrefix;//xy_walk    std::string output;};tinyxml2::XMLDocument doc;void handleFrameValue(tinyxml2::XMLElement * arrayDictNode, int id){    printf("%s\n", arrayDictNode->Name());    tinyxml2::XMLElement * childNode = arrayDictNode->FirstChildElement();        while(childNode){        printf("***** %s\n", childNode->GetText());        std::string time = "time";        childNode = childNode->NextSiblingElement();    }    //    }tinyxml2::XMLElement * addFirstKey(tinyxml2::XMLElement * parent, const char * keyName, const char * keyValueName, const char * value){    tinyxml2::XMLElement * keyNode = doc.NewElement("key");    keyNode->InsertFirstChild(doc.NewText(keyName));    parent->InsertFirstChild(keyNode);        tinyxml2::XMLElement * nameValue = doc.NewElement(keyValueName);    nameValue->SetName(keyValueName);    if(value){        nameValue->InsertFirstChild(doc.NewText(value));    }        parent->InsertAfterChild(keyNode, nameValue);    return nameValue;}tinyxml2::XMLElement * addKey(tinyxml2::XMLElement * parent, tinyxml2::XMLElement * node, const char * keyName, const char * keyValueName, const char * value){    tinyxml2::XMLElement * keyNode = doc.NewElement("key");    keyNode->InsertFirstChild(doc.NewText(keyName));    parent->InsertAfterChild(node, keyNode);        tinyxml2::XMLElement * nameValue = doc.NewElement(keyValueName);    nameValue->SetName(keyValueName);    if(value){        nameValue->InsertFirstChild(doc.NewText(value));    }        parent->InsertAfterChild(keyNode, nameValue);    return nameValue;}tinyxml2::XMLElement * addFirstValue(tinyxml2::XMLElement * parent, const char * valueName, const char * value){    tinyxml2::XMLElement * nameValue = doc.NewElement(valueName);    nameValue->SetValue(valueName);    if(value){        nameValue->InsertFirstChild(doc.NewText(value));    }        parent->InsertFirstChild(nameValue);    return nameValue;}tinyxml2::XMLElement * addValue(tinyxml2::XMLElement * parent, tinyxml2::XMLElement * nodeBefore, const char * valueName, const char * value){    tinyxml2::XMLElement * nameValue = doc.NewElement(valueName);    nameValue->SetValue(valueName);    if(value){        nameValue->InsertFirstChild(doc.NewText(value));    }        parent->InsertAfterChild(nodeBefore, nameValue);    return nameValue;}void createFrame(tinyxml2::XMLElement * arrayNode, AnimationInfo* info, int id){    tinyxml2::XMLElement * dict = doc.NewElement("dict");    arrayNode->InsertFirstChild(dict);    //easing    tinyxml2::XMLElement * easing = addFirstKey(dict, "easing", "dict", 0);    addFirstKey(easing, "type", "integer", "0");    //name    tinyxml2::XMLElement * name = addKey(dict, easing, "name", "dict", 0);    addFirstKey(name, "type", "integer", "0");    //time    char stime[10];    sprintf(stime, "%.2f", (id-1)*info->interval);    tinyxml2::XMLElement * time = addKey(dict, name, "time", "real", stime);    //type    tinyxml2::XMLElement * type = addKey(dict, time, "type", "integer", "7");    //value    tinyxml2::XMLElement * value = addKey(dict, type, "value", "array", 0);    char spng[100];    sprintf(spng, "%s%d.png", info->filePrefix.c_str(), id);    tinyxml2::XMLElement * png = addFirstValue(value, "string", spng);    tinyxml2::XMLElement * plist = addValue(value, png, "string", info->plistfile.c_str());}void addAnimation(tinyxml2::XMLNode * nodeGraph, AnimationInfo * info){    tinyxml2::XMLElement * node = nodeGraph->NextSiblingElement()->FirstChildElement("key")->NextSiblingElement();    tinyxml2::XMLElement * zeroNode = node->FirstChildElement("key");    tinyxml2::XMLElement * displayName = zeroNode->NextSiblingElement()->FirstChildElement();    tinyxml2::XMLElement * keyframes = displayName->NextSiblingElement()->FirstChildElement();        tinyxml2::XMLElement * arrayNode = keyframes->NextSiblingElement();    arrayNode->DeleteChildren();        for(int i=info->frameCount; i>=1; --i){        createFrame(arrayNode, info, i);    }}void changeDuration(tinyxml2::XMLNode * seq, float duration){    tinyxml2::XMLElement * dict = seq->FirstChildElement("dict");    tinyxml2::XMLElement * node = dict->FirstChildElement("key");    while(node){        if(node->GetText() == std::string("length")){            tinyxml2::XMLElement * length = node->NextSiblingElement();            length->DeleteChildren();            char s[100];            sprintf(s, "%.2f", duration);            length->InsertFirstChild(doc.NewText(s));        }        node = node->NextSiblingElement("key");    }    }#endif

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"><plist version="1.0"><dict><key>centeredOrigin</key><false/><key>currentResolution</key><integer>0</integer><key>currentSequenceId</key><integer>0</integer><key>fileType</key><string>CocosBuilder</string><key>fileVersion</key><integer>4</integer><key>guides</key><array/><key>nodeGraph</key><dict><key>animatedProperties</key><dict>            <key>0</key><dict> <key>displayFrame</key><dict> <key>keyframes</key>                    <array>                         <dict><key>easing</key><dict><key>type</key><integer>0</integer></dict><key>name</key><string>displayFrame</string><key>time</key><real>0.0</real><key>type</key><integer>7</integer><key>value</key><array><string>TaskUI/bg_01.png</string><string>Pic/TaskUI.plist</string></array></dict>                                                <dict><key>easing</key><dict><key>type</key><integer>0</integer></dict><key>name</key><string>displayFrame</string><key>time</key><real>0.5</real><key>type</key><integer>7</integer><key>value</key><array><string>TaskUI/bg_02.png</string><string>Pic/TaskUI.plist</string></array></dict><dict><key>easing</key><dict><key>type</key><integer>0</integer></dict><key>name</key><string>displayFrame</string><key>time</key><real>1</real><key>type</key><integer>7</integer><key>value</key><array><string>TaskUI/bg_03.png</string><string>Pic/TaskUI.plist</string></array></dict><dict><key>easing</key><dict><key>type</key><integer>0</integer></dict><key>name</key><string>displayFrame</string><key>time</key><real>1.5</real><key>type</key><integer>7</integer><key>value</key><array><string>TaskUI/bg_04.png</string><string>Pic/TaskUI.plist</string></array></dict></array><key>name</key><string>displayFrame</string><key>type</key><integer>7</integer></dict></dict></dict><key>baseClass</key><string>CCSprite</string><key>children</key><array/><key>customClass</key><string></string><key>displayName</key><string>CCSprite</string><key>memberVarAssignmentName</key><string></string><key>memberVarAssignmentType</key><integer>0</integer><key>properties</key><array><dict><key>name</key><string>anchorPoint</string><key>type</key><string>Point</string><key>value</key><array><real>0.5</real><real>0.5</real></array></dict><dict><key>name</key><string>scale</string><key>type</key><string>ScaleLock</string><key>value</key><array><real>1</real><real>1</real><false/><integer>0</integer></array></dict><dict><key>name</key><string>ignoreAnchorPointForPosition</string><key>type</key><string>Check</string><key>value</key><false/></dict><dict><key>baseValue</key><array><string>TaskUI/bg_01.png</string><string>Pic/TaskUI.plist</string></array><key>name</key><string>displayFrame</string><key>type</key><string>SpriteFrame</string><key>value</key><array><string>Pic/TaskUI.plist</string><string>TaskUI/bg_04.png</string></array></dict></array><key>seqExpanded</key><true/></dict><key>notes</key><array/><key>resolutions</key><array><dict><key>centeredOrigin</key><false/><key>ext</key><string>ipad hd</string><key>height</key><integer>0</integer><key>name</key><string>iPad</string><key>scale</key><real>2</real><key>width</key><integer>0</integer></dict></array><key>sequences</key><array><dict><key>autoPlay</key><true/><key>chainedSequenceId</key><integer>0</integer><key>length</key><real>2</real><key>name</key><string>Default Timeline</string><key>offset</key><real>0.0</real><key>position</key><real>2</real><key>resolution</key><real>30</real><key>scale</key><real>128</real><key>sequenceId</key><integer>0</integer></dict></array><key>stageBorder</key><integer>3</integer></dict></plist>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.