1.包含tinyxml.lib 及头文件
2.code
void CreateXmlFile()
{
TiXmlDocument *xdoc = new TiXmlDocument(); //创建一个XML的文档对象。
TiXmlDeclaration* xdec = new TiXmlDeclaration("1.0", "gbk", ""); //创建XMl文件头
xdoc->LinkEndChild(xdec); //将文件头连接到文档对象
TiXmlElement *Ele1 = new TiXmlElement("business"); //创建business对象
xdoc->LinkEndChild(Ele1); //将business对象连接到文档对象
Ele1->SetAttribute("id", "FPKJ"); //设置business对象的属性
Ele1->SetAttribute("comment", "工具");
TiXmlElement *Ele2 = new TiXmlElement("REQUEST_COMMON_FPKJ"); //创建REQUEST_COMMON_FPKJ对象
Ele1->LinkEndChild(Ele2); //将REQUEST_COMMON_FPKJ子对象连接到business对象
Ele2->SetAttribute("class", "REQUEST_COMMON_FPKJ");
TiXmlElement *Ele3 = new TiXmlElement("COMMON_FPKJ_FPT");
Ele2->LinkEndChild(Ele3);
Ele3->SetAttribute("class", "COMMON_FPKJ_FPT");
TiXmlElement* FPQQLSH = new TiXmlElement("FPQQLSH");
TiXmlText* FPQQLSH_text = new TiXmlText("123"); //设置值
FPQQLSH->LinkEndChild(FPQQLSH_text);
Ele3->LinkEndChild(FPQQLSH);
TiXmlElement* KPLX = new TiXmlElement("KPLX");
TiXmlText* KPLX_text = new TiXmlText("234");
KPLX->LinkEndChild(KPLX_text);
Ele3->LinkEndChild(KPLX);
TiXmlElement *Ele4 = new TiXmlElement("COMMON_FPKJ_XMXXS");
Ele2->LinkEndChild(Ele4);
Ele4->SetAttribute("class", "COMMON_FPKJ_XMXX");
Ele4->SetAttribute("size", 2);
for (int i = 0; i < 2; i++)
{
TiXmlElement *Ele5 = new TiXmlElement("COMMON_FPKJ_XMXX");
Ele4->LinkEndChild(Ele5);
TiXmlElement* FPHXZ = new TiXmlElement("FPHXZ");
TiXmlText* FPHXZ_text = new TiXmlText("345");
FPHXZ->LinkEndChild(FPHXZ_text);
Ele5->LinkEndChild(FPHXZ);
TiXmlElement* SPBM = new TiXmlElement("SPBM");
TiXmlText* SPBM_text = new TiXmlText("456");
SPBM->LinkEndChild(SPBM_text);
Ele5->LinkEndChild(SPBM);
}
xdoc->SaveFile("1.xml");//保存到文件
}
3.结果
<?xml version="1.0" encoding="gbk" ?>
<business id="FPKJ" comment="工具">
<REQUEST_COMMON_FPKJ class="REQUEST_COMMON_FPKJ">
<COMMON_FPKJ_FPT class="COMMON_FPKJ_FPT">
<FPQQLSH>123</FPQQLSH>
<KPLX>234</KPLX>
</COMMON_FPKJ_FPT>
<COMMON_FPKJ_XMXXS class="COMMON_FPKJ_XMXX" size="2">
<COMMON_FPKJ_XMXX>
<FPHXZ>345</FPHXZ>
<SPBM>456</SPBM>
</COMMON_FPKJ_XMXX>
<COMMON_FPKJ_XMXX>
<FPHXZ>345</FPHXZ>
<SPBM>456</SPBM>
</COMMON_FPKJ_XMXX>
</COMMON_FPKJ_XMXXS>
</REQUEST_COMMON_FPKJ>
</business>