微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

TinyXML生成xml文件

注意:

InsertEndChild与LinkEndChild区别
Insert 系列的函数插入的是结点的副本(包括所有子结点),而 LinkEndChild 插入的就是你创建的对象。

例子 xml 内容
<?xml version="1.0" encoding="UTF-8" ?>
<Config>
<Database ip="192.168.1.33" port="3306" />
<List>
<Channel count="5">电视剧</Channel>
<Channel count="5">电影</Channel>
</List>
</Config>

写法一:

  1. voidCxmlDlg::MakeXML1()
  2. {
  3. //生成XML内容
  4. tixmlDocumentdoc;
  5. tixmlElementconfig("Config");
  6. tixmlElementdatabase("Database");
  7. database.SetAttribute("ip","192.168.1.33");
  8. database.SetAttribute("port",3306);
  9. config.InsertEndChild(database);
  10. tixmlElementlist("List");
  11. charutf8[32]={0};
  12. tixmlElementchannel1("Channel");
  13. channel1.SetAttribute("count",5);
  14. MBSToUTF8(utf8,sizeof(utf8),"电视剧");
  15. tixmlTexttext1(utf8);
  16. channel1.InsertEndChild(text1);
  17. list.InsertEndChild(channel1);
  18. tixmlElementchannel2("Channel");
  19. channel2.SetAttribute("count","电影");
  20. tixmlTexttext2(utf8);
  21. channel2.InsertEndChild(text2);
  22. list.InsertEndChild(channel2);
  23. config.InsertEndChild(list);
  24. doc.InsertEndChild(config);
  25. tixmlPrinterprinter;
  26. printer.SetIndent(0);//设置缩进字符,设为0表示不使用缩进。认为4个空格,也可设为'\t'
  27. doc.Accept(&printer);
  28. charcontent[256]={0};
  29. intsize=printer.Size();
  30. assert(size<sizeof(content));
  31. strcpy_s(content,153); background-color:inherit; font-weight:bold">sizeof(content),printer.Cstr());
  32. }


写法二:

voidCxmlDlg::MakeXML2()
  • tixmlDocument*doc=newtixmlDocument();
  • tixmlElement*config=newtixmlElement("Config");
  • tixmlElement*database=newtixmlElement("Database");
  • database->SetAttribute("ip",248); line-height:14px"> database->SetAttribute("port",248); line-height:14px"> config->LinkEndChild(database);
  • tixmlElement*list=newtixmlElement("List");
  • tixmlElement*channel1=newtixmlElement("Channel");
  • channel1->SetAttribute("count",248); line-height:14px"> tixmlText*text1=newtixmlText(utf8);
  • channel1->LinkEndChild(text1);
  • list->LinkEndChild(channel1);
  • tixmlElement*channel2= channel2->SetAttribute("count",248); line-height:14px"> tixmlText*text2= channel2->LinkEndChild(text2);
  • list->LinkEndChild(channel2);
  • config->LinkEndChild(list);
  • doc->LinkEndChild(config);
  • printer.SetIndent(0);
  • doc->Accept(&printer);
  • charcontent[512]={0};
  • memcpy(content,printer.Cstr(),printer.Size());
  • deletedoc;
  • }
  • 版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。