TinyXml 追加到 xml 文件

发布于 2024-12-14 03:36:52 字数 2419 浏览 1 评论 0原文

我已经在一个项目上工作了几个月了,我现在遇到的主要问题是附加到 xml 文件。我可以毫无问题地创建文件。但我希望能够添加更多数据。基本上就像一个小型数据库一样使用它。

包含的代码只是整个程序的一小部分,但我相信它在这里,我需要帮助来找出附加到文件的过程。每个元素都可以毫无问题地接受用户输入。但是当他们去写第二个Horse tot时,他的文件会删除旧的文件并创建一个全新的文件。

关于如何附加的任何建议都会很棒

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心凉怎暖 2024-12-21 03:36:52

抱歉第一次发帖,刚刚习惯网站布局。

我如何解决 xml 文件的附加问题。
事实证明这很简单。我将使用上面的例子。

它不是最干净的代码,但该示例仍然适用。
现在它可以工作了,我可以稍微整理一下代码。

我只是希望这可以帮助其他人了解如何做到这一点。

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 

}

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 

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文