YUIYDUI commited on
Commit
a5bf50f
1 Parent(s): 8ceb356

XML_test uoload

Browse files
Files changed (1) hide show
  1. main.cpp +187 -0
main.cpp ADDED
@@ -0,0 +1,187 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include <iostream>
2
+ #include"tinyxml2.h"
3
+ #include<string>
4
+
5
+ using namespace std;
6
+ using namespace tinyxml2;
7
+ string a;
8
+ string b;
9
+ string c;
10
+ string d;
11
+ string a1;
12
+
13
+
14
+ void createXml()
15
+ {
16
+ //------------
17
+ //声明要创建的xml文件
18
+ //------------
19
+ tinyxml2::XMLDocument xml;
20
+ tinyxml2::XMLDeclaration* declaration = xml.NewDeclaration();
21
+ xml.InsertFirstChild(declaration);
22
+
23
+ //------------
24
+ //创建根节点
25
+ //------------
26
+ tinyxml2::XMLElement* rootNode = xml.NewElement("computer");
27
+ xml.InsertEndChild(rootNode);
28
+
29
+ //------------
30
+ //创建子节点
31
+ //------------
32
+ tinyxml2::XMLElement* root_1_NodeComputerMonitor = xml.NewElement("ComputerMonitor");
33
+ tinyxml2::XMLElement* root_1_NodeKeyboard = xml.NewElement("Keyboard");
34
+ tinyxml2::XMLElement* root_1_CPU = xml.NewElement("CPU");
35
+
36
+ //------------
37
+ //给子节点增加内容
38
+ //------------
39
+ tinyxml2::XMLText* text_root_1_NodeComputerMonitor = xml.NewText("SAMSUNG");
40
+ root_1_NodeComputerMonitor->InsertFirstChild(text_root_1_NodeComputerMonitor);
41
+
42
+ tinyxml2::XMLText* text_root_1_root_1_CPU = xml.NewText("intel");
43
+ root_1_CPU->InsertFirstChild(text_root_1_root_1_CPU);
44
+
45
+ //------------
46
+ //给子节点增加内容
47
+ //------------
48
+ root_1_NodeComputerMonitor->SetAttribute("size", "15");
49
+ root_1_CPU->SetAttribute("series", "XEON");
50
+
51
+ //------------
52
+ //创建子节点的子节点
53
+ //------------
54
+ tinyxml2::XMLElement* root_2_NodeKeyboardAttribute = xml.NewElement("KeyboardAttribute");
55
+
56
+ //------------
57
+ //给子节点的子节点增加内容
58
+ //------------
59
+ tinyxml2::XMLText* text_root_2_NodeKeyboardAttribute = xml.NewText("cherry Mechanical Keyboard");
60
+ root_2_NodeKeyboardAttribute->InsertFirstChild(text_root_2_NodeKeyboardAttribute);
61
+
62
+ //------------
63
+ //构建xml树,根节点的下的子节点树
64
+ //------------
65
+ rootNode->InsertEndChild(root_1_NodeComputerMonitor);//childNodeStu是root的子节点
66
+ rootNode->InsertEndChild(root_1_NodeKeyboard);
67
+ rootNode->InsertEndChild(root_1_CPU);
68
+ //------------
69
+ //构建xml树,根节点的下的子节点的子节点树
70
+ //------------
71
+ root_1_NodeKeyboard->InsertEndChild(root_2_NodeKeyboardAttribute);
72
+
73
+ //------------
74
+ //将xml保存到当前项目中
75
+ //------------
76
+ xml.SaveFile("computer.xml");
77
+ }
78
+
79
+ void deCodeXml()
80
+ {
81
+ //------------
82
+ //声明
83
+ //------------
84
+ tinyxml2::XMLDocument xml;
85
+
86
+ //------------
87
+ //导入xml文件
88
+ //------------
89
+ if (xml.LoadFile("computer.xml") != XML_SUCCESS)
90
+ {
91
+ return;
92
+ }
93
+
94
+ //------------
95
+ //找到导入的xml的根节点
96
+ //------------
97
+ tinyxml2::XMLElement* rootNode = xml.RootElement();
98
+ if (rootNode == NULL) {
99
+ return;
100
+ }
101
+
102
+ //------------
103
+ //读取第一层子节点信息
104
+ //------------
105
+ tinyxml2::XMLElement* root_1_NodeComputerMonitor = rootNode->FirstChildElement("ComputerMonitor");
106
+ std::string text_root_1_NodeComputerMonitor = root_1_NodeComputerMonitor->GetText();
107
+ std::string text_root_1_size = root_1_NodeComputerMonitor->Attribute("size");
108
+ a=text_root_1_NodeComputerMonitor.c_str();
109
+ a1=text_root_1_size;
110
+
111
+
112
+ //------------
113
+ //读取第二层子节点信息
114
+ //------------
115
+ tinyxml2::XMLElement* root_1_NodeKeyboard = rootNode->FirstChildElement("Keyboard");
116
+ tinyxml2::XMLElement* root_2_NodeKeyboardAttribute = root_1_NodeKeyboard->FirstChildElement("KeyboardAttribute");
117
+ std::string text_root_2_NodeKeyboardAttribute = root_2_NodeKeyboardAttribute->GetText();
118
+ b = text_root_2_NodeKeyboardAttribute.c_str();
119
+
120
+ tinyxml2::XMLElement* root_1_NodeCPU = rootNode->FirstChildElement("CPU");
121
+ std::string text_root_1_NodeCPU = root_1_NodeCPU->GetText();
122
+ std::string text_root_1_series = root_1_NodeCPU->Attribute("series");
123
+ c=text_root_1_series;
124
+ d=text_root_1_NodeCPU;
125
+ }
126
+ // <?xml version="1.0" encoding="UTF-8"?>
127
+ // <computer>
128
+ // <ComputerMonitor size="15">SAMSUNG</ComputerMonitor>
129
+ // <Keyboard>
130
+ // <KeyboardAttribute>cherry Mechanical Keyboard</KeyboardAttribute>
131
+ // </Keyboard>
132
+ // <CPU series="XEON">intel</CPU>
133
+ // </computer>
134
+ struct ComputerMonitor
135
+ {
136
+ string size;
137
+ string type;
138
+ };
139
+ struct CPU
140
+ {
141
+ string series;
142
+ string type;
143
+
144
+ };
145
+ struct computer
146
+ {
147
+ struct ComputerMonitor cm1 ;
148
+ string Keyboard;
149
+ struct CPU cpu1;
150
+
151
+ };
152
+ int main()
153
+ {
154
+ // cout << "----------------------begin create xml-----------------------" << endl;
155
+ // createXml();
156
+ // cout << "----------------------finished create xml--------------------" << endl;
157
+
158
+
159
+
160
+ // cout << "-----------------------begin read xml------------------------" << endl;
161
+ deCodeXml();
162
+ // cout << "-----------------------finished read xml---------------------" << endl;
163
+
164
+
165
+ struct computer computer1;
166
+ computer1.cm1.size=a ;
167
+ computer1.cm1.type=a1;
168
+ computer1.Keyboard = b;
169
+ computer1.cpu1.series=c;
170
+ computer1.cpu1.type=d;
171
+ cout<<computer1.cm1.size<<endl;
172
+ cout<<computer1.cm1.type<<endl;
173
+ cout<<computer1.Keyboard<<endl;
174
+ cout<<computer1.cpu1.series<<endl;
175
+ cout<<computer1.cpu1.type<<endl;
176
+ return 0;
177
+
178
+ }
179
+
180
+
181
+
182
+
183
+
184
+
185
+
186
+
187
+