注意:
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;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。