TinyXML生成xml文件

本文对比了 TiXML 中 InsertEndChild 和 LinkEndChild 的使用方法及区别,前者复制结点及其子结点,后者直接链接指定对象。通过具体 XML 生成实例展示了两种方法的具体应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

注意:

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. void CxmlDlg::MakeXML1()  
  2. {  
  3.     // 生成 XML 内容  
  4.     TiXmlDocument doc;  
  5.   
  6.     TiXmlElement config("Config");  
  7.   
  8.     TiXmlElement database("Database");  
  9.     database.SetAttribute( "ip""192.168.1.33" );  
  10.     database.SetAttribute( "port", 3306 );  
  11.   
  12.     config.InsertEndChild( database );  
  13.   
  14.     TiXmlElement list("List");  
  15.   
  16.     char utf8[32] = {0};  
  17.   
  18.     TiXmlElement channel1("Channel");  
  19.     channel1.SetAttribute( "count", 5 );  
  20.   
  21.     MBSToUTF8( utf8, sizeof(utf8), "电视剧" );  
  22.     TiXmlText text1( utf8 );  
  23.     channel1.InsertEndChild( text1 );  
  24.     list.InsertEndChild( channel1 );  
  25.   
  26.     TiXmlElement channel2("Channel");  
  27.     channel2.SetAttribute( "count", 5 );  
  28.   
  29.     MBSToUTF8( utf8, sizeof(utf8), "电影" );  
  30.     TiXmlText text2( utf8 );  
  31.     channel2.InsertEndChild( text2 );  
  32.     list.InsertEndChild( channel2 );  
  33.   
  34.     config.InsertEndChild( list );  
  35.     doc.InsertEndChild( config );  
  36.   
  37.     TiXmlPrinter printer;  
  38.     printer.SetIndent( 0 ); // 设置缩进字符,设为 0 表示不使用缩进。默认为 4个空格,也可设为'\t'  
  39.     doc.Accept( &printer );  
  40.   
  41.     char content[256] = {0};  
  42.     int size = printer.Size();  
  43.     assert( size < sizeof(content) );  
  44.     strcpy_s( content, sizeof(content), printer.CStr() );  
  45. }  


 写法二:

[cpp]  view plain copy
  1. void CxmlDlg::MakeXML2()  
  2. {  
  3.     // 生成 XML 内容  
  4.     TiXmlDocument *doc = new TiXmlDocument();  
  5.   
  6.     TiXmlElement *config = new TiXmlElement("Config");  
  7.   
  8.     TiXmlElement *database = new TiXmlElement("Database");  
  9.     database->SetAttribute( "ip""192.168.1.33" );  
  10.     database->SetAttribute( "port", 3306 );  
  11.   
  12.     config->LinkEndChild( database );  
  13.   
  14.     TiXmlElement *list = new TiXmlElement("List");  
  15.   
  16.     char utf8[32] = {0};  
  17.   
  18.     TiXmlElement *channel1 = new TiXmlElement("Channel");  
  19.     channel1->SetAttribute( "count", 5 );  
  20.   
  21.     MBSToUTF8( utf8, sizeof(utf8), "电视剧" );  
  22.     TiXmlText *text1 = new TiXmlText( utf8 );  
  23.     channel1->LinkEndChild( text1 );  
  24.     list->LinkEndChild( channel1 );  
  25.   
  26.     TiXmlElement *channel2 = new TiXmlElement("Channel");  
  27.     channel2->SetAttribute( "count", 5 );  
  28.   
  29.     MBSToUTF8( utf8, sizeof(utf8), "电影" );  
  30.     TiXmlText *text2 = new TiXmlText( utf8 );  
  31.     channel2->LinkEndChild( text2 );  
  32.     list->LinkEndChild( channel2 );  
  33.   
  34.     config->LinkEndChild( list );  
  35.     doc->LinkEndChild( config );  
  36.   
  37.     TiXmlPrinter printer;  
  38.     printer.SetIndent( 0 );  
  39.     doc->Accept( &printer );  
  40.   
  41.     char content[512] = {0};  
  42.     int size = printer.Size();  
  43.     assert( size < sizeof(content) );  
  44.     memcpy( content, printer.CStr(), printer.Size() );  
  45.   
  46.     delete doc;  
  47. }  

 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值