File size: 4,877 Bytes
a5bf50f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include <iostream>
#include"tinyxml2.h"
#include<string>

using namespace std;
using namespace tinyxml2;
string a;
string b;
string c;
string d;
string a1;


void createXml()
{
	//------------
	//声明要创建的xml文件
	//------------
	tinyxml2::XMLDocument xml;
	tinyxml2::XMLDeclaration* declaration = xml.NewDeclaration();
	xml.InsertFirstChild(declaration);

	//------------
	//创建根节点
	//------------
	tinyxml2::XMLElement* rootNode = xml.NewElement("computer");
	xml.InsertEndChild(rootNode);

	//------------
	//创建子节点
	//------------
	tinyxml2::XMLElement* root_1_NodeComputerMonitor = xml.NewElement("ComputerMonitor");
	tinyxml2::XMLElement* root_1_NodeKeyboard = xml.NewElement("Keyboard");
	tinyxml2::XMLElement* root_1_CPU = xml.NewElement("CPU");

	//------------
	//给子节点增加内容
	//------------
	tinyxml2::XMLText* text_root_1_NodeComputerMonitor = xml.NewText("SAMSUNG");
	root_1_NodeComputerMonitor->InsertFirstChild(text_root_1_NodeComputerMonitor);

	tinyxml2::XMLText* text_root_1_root_1_CPU = xml.NewText("intel");
	root_1_CPU->InsertFirstChild(text_root_1_root_1_CPU);

	//------------
	//给子节点增加内容
	//------------
	root_1_NodeComputerMonitor->SetAttribute("size", "15");
	root_1_CPU->SetAttribute("series", "XEON");

	//------------
	//创建子节点的子节点
	//------------
	tinyxml2::XMLElement* root_2_NodeKeyboardAttribute = xml.NewElement("KeyboardAttribute");
	
	//------------
	//给子节点的子节点增加内容
	//------------
	tinyxml2::XMLText* text_root_2_NodeKeyboardAttribute = xml.NewText("cherry Mechanical Keyboard");
	root_2_NodeKeyboardAttribute->InsertFirstChild(text_root_2_NodeKeyboardAttribute);

	//------------
	//构建xml树,根节点的下的子节点树
	//------------
	rootNode->InsertEndChild(root_1_NodeComputerMonitor);//childNodeStu是root的子节点
	rootNode->InsertEndChild(root_1_NodeKeyboard);
	rootNode->InsertEndChild(root_1_CPU);
	//------------
	//构建xml树,根节点的下的子节点的子节点树
	//------------
	root_1_NodeKeyboard->InsertEndChild(root_2_NodeKeyboardAttribute);

	//------------
	//将xml保存到当前项目中
	//------------
	xml.SaveFile("computer.xml");
}
 
void deCodeXml()
{
	//------------
	//声明
	//------------
	tinyxml2::XMLDocument xml;

	//------------
	//导入xml文件
	//------------
	if (xml.LoadFile("computer.xml") != XML_SUCCESS)
	{
		return;
	}

	//------------
	//找到导入的xml的根节点
	//------------
	tinyxml2::XMLElement* rootNode = xml.RootElement();
	if (rootNode == NULL) {
		return;
	}
	
	//------------
	//读取第一层子节点信息
	//------------
	tinyxml2::XMLElement* root_1_NodeComputerMonitor = rootNode->FirstChildElement("ComputerMonitor");
	std::string text_root_1_NodeComputerMonitor = root_1_NodeComputerMonitor->GetText();
	std::string text_root_1_size = root_1_NodeComputerMonitor->Attribute("size");
	a=text_root_1_NodeComputerMonitor.c_str();
	a1=text_root_1_size;
	

	//------------
	//读取第二层子节点信息
	//------------
	tinyxml2::XMLElement* root_1_NodeKeyboard = rootNode->FirstChildElement("Keyboard");
	tinyxml2::XMLElement* root_2_NodeKeyboardAttribute = root_1_NodeKeyboard->FirstChildElement("KeyboardAttribute");
	std::string text_root_2_NodeKeyboardAttribute = root_2_NodeKeyboardAttribute->GetText();
	b = text_root_2_NodeKeyboardAttribute.c_str();

	tinyxml2::XMLElement* root_1_NodeCPU = rootNode->FirstChildElement("CPU");
	std::string text_root_1_NodeCPU = root_1_NodeCPU->GetText();
	std::string text_root_1_series = root_1_NodeCPU->Attribute("series");
	c=text_root_1_series;
	d=text_root_1_NodeCPU;
}
// <?xml version="1.0" encoding="UTF-8"?>
// <computer>
//     <ComputerMonitor size="15">SAMSUNG</ComputerMonitor>
//     <Keyboard>
//         <KeyboardAttribute>cherry Mechanical Keyboard</KeyboardAttribute>
//     </Keyboard>
//     <CPU series="XEON">intel</CPU>
// </computer>
struct ComputerMonitor
	{
		string size;
		string type;
	};
struct CPU
	{
		string series;
		string type;

	};
struct computer
	{
		struct ComputerMonitor cm1 ;
		string Keyboard;
		struct CPU cpu1;

	};
int main()
{
	// cout << "----------------------begin create xml-----------------------" << endl;
	// createXml();
	// cout << "----------------------finished create xml--------------------" << endl;



	// cout << "-----------------------begin read xml------------------------" << endl;
	deCodeXml();
	// cout << "-----------------------finished read xml---------------------" << endl;
    
	
	struct computer computer1;
	computer1.cm1.size=a ;
	computer1.cm1.type=a1;
	computer1.Keyboard = b;
	computer1.cpu1.series=c;
	computer1.cpu1.type=d;
	cout<<computer1.cm1.size<<endl;
	cout<<computer1.cm1.type<<endl;
	cout<<computer1.Keyboard<<endl;
	cout<<computer1.cpu1.series<<endl;
	cout<<computer1.cpu1.type<<endl;
	return 0;
	
}