rapidxml往xml文件循环写入内容
生活随笔
收集整理的這篇文章主要介紹了
rapidxml往xml文件循环写入内容
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
初次使用rapidxml往xml文件中循環(huán)寫入內(nèi)容,以下是錯誤演示:
bool StudentMgr::save () {///創(chuàng)建文件操作對象rapidxml::xml_document<> doc;myNode* root = doc.allocate_node(rapidxml::node_pi,doc.allocate_string("xml version='1.0' encoding='gb2312'"));doc.append_node(root);myNode* StudentInformation = doc.allocate_node(rapidxml::node_element,"StudentInformation",nullptr);myNode* StudentList = doc.allocate_node(rapidxml::node_element,"StudentList",nullptr);doc.append_node(StudentInformation);StudentInformation->append_node(StudentList);MapStudent::iterator it;char str[1024];const char* name;const char* sex;const char* id;const char* telnumber;int iter=0;///取出容器內(nèi)容for (it = _students.begin (); it != _students.end (); it++){name = it->second._name.data();sex = it->second._sex.data();id = it->second._id.data();telnumber = it->second._telnumber.data();sprintf_s(str,"%s %s %s %s",name,sex,id,telnumber);StudentList->append_node(doc.allocate_node(rapidxml::node_element, "Student",str));}///打開文件并寫入xml信息std::string text;rapidxml::print(back_inserter(text), doc, 0);std::ofstream out(_fileName);out<<doc;return true; }代碼中未定義的變量為類的成員變量已在容器中賦值,就不再贅述,以上代碼能通過編譯也能運行,本函數(shù)是想實現(xiàn)將容器中的成員變量循環(huán)賦值給student節(jié)點,然后重定義該xml文件內(nèi)容;但真實效果卻是student的每一行都為容器中最后的元素的值。
正確的代碼只需要將上圖代碼中的
StudentList->append_node(doc.allocate_node(rapidxml::node_element, "Student",str));修改為
StudentList->append_node(doc.allocate_node(rapidxml::node_element, "Student", doc.allocate_string(str, 1024)));其中巨細(xì)暫時還沒有探究,現(xiàn)只解決了這個問題,希望有了解的大佬可以講解一下,不勝感激。
總結(jié)
以上是生活随笔為你收集整理的rapidxml往xml文件循环写入内容的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 初学java socket编程实例代码讲
- 下一篇: linux ftp服务器登录异常解决