
int Write(){ tixmldocument doc ; tixmlDeclaration *declare =new tixmlDeclaration("1.0","",""); doc.linkEndChild(declare); doc.linkEndChild(new tixmlComment("群英集团人力资源表")); tixmlElement *root = new tixmlElement("群英集团"); tixmlElement *sub = new tixmlElement("员工"); sub->SetAttribute("ID","011"); // 向sub中添加属性 sub->SetAttribute("职位","技术总监"); tixmlElement *child = new tixmlElement("姓名"); // 建立子元素 tixmlText *content =new tixmlText("虚竹"); // 建立文本 child->linkEndChild(content); // 将建立的文本追加到child所指的子元素中 sub->linkEndChild(child); // 将child追加到sub中,以作为子元素 root->linkEndChild(sub); // 将sub追加到root中,以作为子元素 sub = new tixmlElement("员工"); sub->SetAttribute("ID","029"); sub->SetAttribute("职位","技术总监"); child = new tixmlElement("姓名"); content =new tixmlText("乔峰"); child->linkEndChild(content); sub->linkEndChild(child); root->linkEndChild(sub); sub = new tixmlElement("员工"); sub->SetAttribute("ID","100"); sub->SetAttribute("职位","总架构师"); child = new tixmlElement("姓名"); content =new tixmlText("扫地僧"); child->linkEndChild(content); sub->linkEndChild(child); root->linkEndChild(sub); sub = new tixmlElement("员工"); sub->SetAttribute("ID","101"); sub->SetAttribute("职位","公关部经理"); child = new tixmlElement("姓名"); content =new tixmlText("韦小宝"); child->linkEndChild(content); sub->linkEndChild(child); root->linkEndChild(sub); sub = new tixmlElement("员工"); sub->SetAttribute("ID","102"); sub->SetAttribute("职位","人事部经理"); child = new tixmlElement("姓名"); content =new tixmlText("黄蓉"); child->linkEndChild(content); sub->linkEndChild(child); root->linkEndChild(sub); doc.linkEndChild(root); doc.Savefile("WriteTest.xml"); return 0;} 输出效果:
<?xml version="1.0" ?><!--群英集团人力资源表--><群英集团> <员工 ID="011" 职位="技术总监"> <姓名>虚竹</姓名> </员工> <员工 ID="029" 职位="技术总监"> <姓名>乔峰</姓名> </员工> <员工 ID="100" 职位="总架构师"> <姓名>扫地僧</姓名> </员工> <员工 ID="101" 职位="公关部经理"> <姓名>韦小宝</姓名> </员工> <员工 ID="102" 职位="人事部经理"> <姓名>黄蓉</姓名> </员工></群英集团>
注意:
在网上搜索如何用TinyXml时,本人普遍的发现了类似如下的代码
tixmldocument doc;tixmlElement *ele = new tixmlElement("test");doc.linkEndChild(ele);doc.Savefile("test.xml"); 也就是只有new而没有delete。
于是当我第一次写的时候,就很守规矩的按部就班的在doc.Savefile后面加上了delete ele,而这一加就把问题加出来了,程序直接在运行时崩掉了。
后来才知道,人家那样写是有原因的。当析构时,tinyxml会对所有已经连接进来的节点进行释放,所以不需要手动的去释放所new出来的东西,而如果tixmldocument对象也是new出来的,则需要对tixmldocument对象执行delete。 总结
以上是内存溢出为你收集整理的练习TinyXml的写 *** 作全部内容,希望文章能够帮你解决练习TinyXml的写 *** 作所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)