注意:
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>
写法一:
- voidCxmlDlg::MakeXML1()
- {
- //生成XML内容
- TiXmlDocumentdoc;
- TiXmlElementconfig("Config");
- TiXmlElementdatabase("Database");
- database.SetAttribute("ip","192.168.1.33");
- database.SetAttribute("port",3306);
- config.InsertEndChild(database);
- TiXmlElementlist("List");
- charutf8[32]={0};
- TiXmlElementchannel1("Channel");
- channel1.SetAttribute("count",5);
- MBSToUTF8(utf8,sizeof(utf8),"电视剧");
- TiXmlTexttext1(utf8);
- channel1.InsertEndChild(text1);
- list.InsertEndChild(channel1);
- TiXmlElementchannel2("Channel");
- channel2.SetAttribute("count","电影");
- TiXmlTexttext2(utf8);
- channel2.InsertEndChild(text2);
- list.InsertEndChild(channel2);
- config.InsertEndChild(list);
- doc.InsertEndChild(config);
- TiXmlPrinterprinter;
- printer.SetIndent(0);//设置缩进字符,设为0表示不使用缩进。默认为4个空格,也可设为'\t'
- doc.Accept(&printer);
- charcontent[256]={0};
- intsize=printer.Size();
- assert(size<sizeof(content));
- strcpy_s(content,153); background-color:inherit; font-weight:bold">sizeof(content),printer.CStr());
- }
写法二:
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;
}
原文链接:https://www.f2er.com/xml/300151.html