在C++中使用TinyXML2解析xml

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

#ifndef AnimationMake_template_h
#define AnimationMake_template_h
#include 
#include 
#include 
#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





	centeredOrigin
	
	currentResolution
	0
	currentSequenceId
	0
	fileType
	CocosBuilder
	fileVersion
	4
	guides
	
	nodeGraph
	
		animatedProperties
		
            
			0
			 
				displayFrame
				 
					keyframes
                     
                        
							easing
							
								type
								0
							
							name
							displayFrame
							time
							0.0
							type
							7
							value
							
								TaskUI/bg_01.png
								Pic/TaskUI.plist
							
						
                        
                        
						
							easing
							
								type
								0
							
							name
							displayFrame
							time
							0.5
							type
							7
							value
							
								TaskUI/bg_02.png
								Pic/TaskUI.plist
							
						
						
							easing
							
								type
								0
							
							name
							displayFrame
							time
							1
							type
							7
							value
							
								TaskUI/bg_03.png
								Pic/TaskUI.plist
							
						
						
							easing
							
								type
								0
							
							name
							displayFrame
							time
							1.5
							type
							7
							value
							
								TaskUI/bg_04.png
								Pic/TaskUI.plist
							
						
					
					name
					displayFrame
					type
					7
				
			
		
		baseClass
		CCSprite
		children
		
		customClass
		
		displayName
		CCSprite
		memberVarAssignmentName
		
		memberVarAssignmentType
		0
		properties
		
			
				name
				anchorPoint
				type
				Point
				value
				
					0.5
					0.5
				
			
			
				name
				scale
				type
				ScaleLock
				value
				
					1
					1
					
					0
				
			
			
				name
				ignoreAnchorPointForPosition
				type
				Check
				value
				
			
			
				baseValue
				
					TaskUI/bg_01.png
					Pic/TaskUI.plist
				
				name
				displayFrame
				type
				SpriteFrame
				value
				
					Pic/TaskUI.plist
					TaskUI/bg_04.png
				
			
		
		seqExpanded
		
	
	notes
	
	resolutions
	
		
			centeredOrigin
			
			ext
			ipad hd
			height
			0
			name
			iPad
			scale
			2
			width
			0
		
	
	sequences
	
		
			autoPlay
			
			chainedSequenceId
			0
			length
			2
			name
			Default Timeline
			offset
			0.0
			position
			2
			resolution
			30
			scale
			128
			sequenceId
			0
		
	
	stageBorder
	3



你可能感兴趣的:(Cocos2d-X,C/C++/汇编)