Search code examples
appendtinyxml

TinyXml append to xml file


I have been working on a project for a few months now and my main issue i am having right now is appending to an xml file. I can create the files no problem. But i want to be able to add more data. Basically use it like a small database.

The code included is only a small part of the whole program but i believe it is in here i need help with to figure out the process to append to the file. Each element takes user input without any issues. but when they go to write a second Horse tot he file is wipes out the old one and make a whole new file.

Any suggestions on how to append would be great

void WriteThisHorseToFile(char* horseName, char* horseMother, char* horseFather, char* horseHeight, 
                      char* horseOwner, char* horseAge, char* horseWins, char* horseMarkings, char* horseNotes)  
{  

TiXmlDocument doc;
TiXmlDeclaration * decl = new TiXmlDeclaration( "1.0", "", "" );  
doc.LinkEndChild( decl );  

TiXmlElement * root = new TiXmlElement( "Horses" );  
doc.LinkEndChild( root );

TiXmlElement * element2 = new TiXmlElement( "Name" );  
root->LinkEndChild( element2 );  

TiXmlText * text2 = new TiXmlText(horseName);  
element2->LinkEndChild( text2 );  

TiXmlElement * element3 = new TiXmlElement( "Mother" );  
element2->LinkEndChild( element3 );  

TiXmlText * text3 = new TiXmlText(horseMother);  
element2->LinkEndChild( text3 );  

TiXmlElement * element4 = new TiXmlElement( "Father" );  
element2->LinkEndChild( element4 );  

TiXmlText * text4 = new TiXmlText(horseFather);  
element2->LinkEndChild( text4 );  

TiXmlElement * element5 = new TiXmlElement( "Height" );  
element2->LinkEndChild( element5 );  

TiXmlText * text5 = new TiXmlText(horseHeight);  
element2->LinkEndChild( text5 ); 

TiXmlElement * element6 = new TiXmlElement( "Owner" );  
element2->LinkEndChild( element6 );  

TiXmlText * text6 = new TiXmlText(horseOwner);  
element2->LinkEndChild( text6 );

TiXmlElement * element7 = new TiXmlElement( "Age" );  
element2->LinkEndChild( element7 );  

TiXmlText * text7 = new TiXmlText(horseAge);  
element2->LinkEndChild( text7 );

TiXmlElement * element8 = new TiXmlElement( "Wins" );  
element2->LinkEndChild( element8 );  

TiXmlText * text8 = new TiXmlText(horseWins);  
element2->LinkEndChild( text8 );

TiXmlElement * element9 = new TiXmlElement( "Markings" );  
element2->LinkEndChild( element9 );  

TiXmlText * text9 = new TiXmlText(horseMarkings);  
element2->LinkEndChild( text9 );

TiXmlElement * element10 = new TiXmlElement( "Notes" );  
element2->LinkEndChild( element10 );  

TiXmlText * text10 = new TiXmlText(horseNotes);  
element2->LinkEndChild( text10 );

dump_to_stdout( &doc );
doc.SaveFile("demo2.xml"); 

PressEnter();
}

Solution

  • Sorry first time posting and just getting used to layout of site.

    How i solved the appending issue for the xml file. turns out it was quite simple. I will make use the example above.

    it is not the cleanest of code but the example still applies. Now that it is working i can tidy up the code a bit.

    I just hope this helps other see how it can be done.

    void WriteThisHorseToFile(char* horseName, char* horseMother, char* horseFather, char* horseHeight, 
                      char* horseOwner, char* horseAge, char* horseWins, char* horseMarkings, char* horseNotes)  
    
    TiXmlDocument doc;
    doc.LoadFile ("horses.xml");
    
    
    TiXmlElement* root = doc.FirstChildElement( "Horses" );
    if ( root )
    {
    
    TiXmlElement * element2 = new TiXmlElement( "Name" );  
    root->LinkEndChild( element2 );  
    
    TiXmlText * text2 = new TiXmlText(horseName);  
    element2->LinkEndChild( text2 );  
    
    //add as many child links as you wish :D
    
    }
    
    else 
    
    {  
    
    TiXmlDocument doc;
    TiXmlDeclaration * decl = new TiXmlDeclaration( "1.0", "", "" );  
    doc.LinkEndChild( decl );  
    
    TiXmlElement * root = new TiXmlElement( "Horses" );  
    doc.LinkEndChild( root );
    
    TiXmlElement * element2 = new TiXmlElement( "Name" );  
    root->LinkEndChild( element2 );  
    
    TiXmlText * text2 = new TiXmlText(horseName);  
    element2->LinkEndChild( text2 ); 
    
    //add as many child links as you wish :D 
    
    }