加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

TinyXML生成xml文件

发布时间:2020-12-16 05:24:28 所属栏目:百科 来源:网络整理
导读:注意: InsertEndChild与LinkEndChild区别 Insert 系列的函数插入的是结点的副本(包括所有子结点),而 LinkEndChild 插入的就是你创建的对象。 例子 xml 内容: ?xml version="1.0" encoding="UTF-8" ? Config Database ip="192.168.1.33" port="3306" / Lis

注意:

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>

写法一:

[cpp] view plain copy
  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;
  • }
  • (编辑:李大同)

    【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

      推荐文章
        热点阅读