-
int Write()
-
{
-
TiXmlDocument doc ;
-
TiXmlDeclaration *declare =new TiXmlDeclaration("1.0" , "","");
-
doc.LinkEndChild(declare);
-
doc.LinkEndChild(new TiXmlComment("群英集团人力资源表"));
-
-
TiXmlElement *root = new TiXmlElement("群英集团");
-
-
TiXmlElement *sub = new TiXmlElement("员工");
-
sub->SetAttribute("ID" , "011"); // 向sub中添加属性
-
sub->SetAttribute("职位" , "技术总监");
-
TiXmlElement *child = new TiXmlElement("姓名"); // 建立子元素
-
TiXmlText *content =new TiXmlText("虚竹"); // 建立文本
-
child->LinkEndChild(content); // 将建立的文本追加到child所指的子元素中
-
sub->LinkEndChild(child); // 将child追加到sub中,以作为子元素
-
root->LinkEndChild(sub); // 将sub追加到root中,以作为子元素
-
-
sub = new TiXmlElement("员工");
-
sub->SetAttribute("ID" , "029");
-
sub->SetAttribute("职位" , "技术总监");
-
child = new TiXmlElement("姓名");
-
content =new TiXmlText("乔峰");
-
child->LinkEndChild(content);
-
sub->LinkEndChild(child);
-
root->LinkEndChild(sub);
-
-
sub = new TiXmlElement("员工");
-
sub->SetAttribute("ID" , "100");
-
sub->SetAttribute("职位" , "总架构师");
-
child = new TiXmlElement("姓名");
-
content =new TiXmlText("扫地僧");
-
child->LinkEndChild(content);
-
sub->LinkEndChild(child);
-
root->LinkEndChild(sub);
-
-
sub = new TiXmlElement("员工");
-
sub->SetAttribute("ID" , "101");
-
sub->SetAttribute("职位" , "公关部经理");
-
child = new TiXmlElement("姓名");
-
content =new TiXmlText("韦小宝");
-
child->LinkEndChild(content);
-
sub->LinkEndChild(child);
-
root->LinkEndChild(sub);
- 效果
-
sub = new TiXmlElement("员工");
-
sub->SetAttribute("ID" , "102");
-
sub->SetAttribute("职位" , "人事部经理");
-
child = new TiXmlElement("姓名");
-
content =new TiXmlText("黄蓉");
-
child->LinkEndChild(content);
-
sub->LinkEndChild(child);
-
root->LinkEndChild(sub);
-
-
doc.LinkEndChild(root);
-
-
doc.SaveFile("WriteTest.xml");
-
-
return 0;
- }
-
<?xml version="1.0" ?>
-
<!--群英集团人力资源表-->
-
<群英集团>
-
<员工 ID="011" 职位="技术总监">
-
<姓名>虚竹</姓名>
-
</员工>
-
<员工 ID="029" 职位="技术总监">
-
<姓名>乔峰</姓名>
-
</员工>
-
<员工 ID="100" 职位="总架构师">
-
<姓名>扫地僧</姓名>
-
</员工>
-
<员工 ID="101" 职位="公关部经理">
-
<姓名>韦小宝</姓名>
-
</员工>
-
<员工 ID="102" 职位="人事部经理">
-
<姓名>黄蓉</姓名>
-
</员工>
- </群英集团>
本文介绍了一个使用TinyXML库创建XML文件的例子。通过代码演示了如何构建XML结构,包括添加声明、注释、根节点及子节点,并设置了属性与文本内容。
715

被折叠的 条评论
为什么被折叠?



