xml的c++生成

 1 #include <stdio.h>
 2 #include <string>
 3 using namespace std;
 4 //1.下载tinyxml并添加到头文件
 5 //2.由父节点LinkEndChild来挂接子节点
 6 #include "../tinyxml/tinyxml.h"  //头文件路径
 7 
 8 int test1()
 9 {
10     TiXmlDocument xml_doc;
11 
12     // 添加XML声明
13     xml_doc.LinkEndChild(new TiXmlDeclaration( "1.0", "GBK", "" ));
14 
15     // 添加根元素
16     TiXmlElement * xml_root = new TiXmlElement("root");
17     xml_doc.LinkEndChild(xml_root);
18 
19     // 在根元素下添加其他子元素
20     if(1)
21     {
22         // 添加host
23         TiXmlElement* xml_child = new TiXmlElement("host"); 
24         xml_root->LinkEndChild(xml_child );
25         // 添加文档内容
26         xml_child->LinkEndChild( new TiXmlText("afanihao.cn"));
27         
28         // 设置属性
29         xml_child->SetAttribute("checked", "true");
30         xml_child->SetAttribute("station", 1001);
31     }
32 
33     if(1)
34     {
35         // 嵌套子元素
36         TiXmlElement* xml_client = new TiXmlElement("client"); 
37         xml_root->LinkEndChild(xml_client );
38 
39         TiXmlElement* xml_clientName = new TiXmlElement("name"); 
40         xml_clientName->LinkEndChild( new TiXmlText("shaofa"));
41         xml_client->LinkEndChild(xml_clientName);
42 
43         TiXmlElement* xml_clientId = new TiXmlElement("id"); 
44         xml_clientId->LinkEndChild( new TiXmlText("200501"));
45         xml_client->LinkEndChild(xml_clientId);
46     }
47     
48 
49     // 1.保存到文件    
50     xml_doc.SaveFile("example02a.xml");
51 
52     // 2.转成string保存
53     string text ;
54     text << xml_doc;
55     return 0;
56 }
57 
58 
59 int main()
60 {
61 
62     test1();
63     return 0;
64 }

 

posted on 2017-12-04 23:58  Doctor_uee  阅读(812)  评论(0)    收藏  举报

导航