forked from dongyusheng/csdn-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo04.cpp
More file actions
218 lines (188 loc) · 8 KB
/
Copy pathdemo04.cpp
File metadata and controls
218 lines (188 loc) · 8 KB
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
//demo04.cpp
//https://github.com/dongyusheng/csdn-code/tree/master/tinyxml2
//增删改查初级版本
#include <iostream>
#include "tinyxml2.h"
#define CHECK_TINYXML2_RESULT(ret) \
do \
{ \
if (ret != tinyxml2::XML_SUCCESS) \
{ \
std::cout << __FUNCTION__ << "(" << __LINE__ << ") failed, ret = " << ret << std::endl; \
return -1; \
} \
} while (0)
/**
* @brief: 写相机属性. 产生完了之后将所有节点挂在p下面
* @param p 某个相机节点
* @param doc 所有的元素属性以及值均在doc的下面
* @param ipAddr IP地址
* @return: 返回说明
* @retval: 返回值说明
*/
static void WriteOneCameraAttibute(tinyxml2::XMLElement *p, tinyxml2::XMLDocument &doc,
const char *ipAddr)
{
const char *subnetMask = "123.45.67.89";
tinyxml2::XMLElement *pDevID = doc.NewElement("DevID");
tinyxml2::XMLText *pDevIdText = doc.NewText("5");
pDevID->InsertEndChild(pDevIdText);
p->InsertEndChild(pDevID);
tinyxml2::XMLElement *pIpAddr = doc.NewElement("IpAddress");
tinyxml2::XMLText *pIpAddrText = doc.NewText(ipAddr);
pIpAddr->InsertEndChild(pIpAddrText);
p->InsertEndChild(pIpAddr);
tinyxml2::XMLElement *pSubnetMask = doc.NewElement("SubnetMask");
tinyxml2::XMLText *pSubnetMaskText = doc.NewText(subnetMask);
pSubnetMask->InsertEndChild(pSubnetMaskText);
p->InsertEndChild(pSubnetMask);
tinyxml2::XMLElement *pExposureAuto = doc.NewElement("ExposureAuto");
tinyxml2::XMLText *pExposureAutoText = doc.NewText("false");
pExposureAuto->InsertEndChild(pExposureAutoText);
p->InsertEndChild(pExposureAuto);
tinyxml2::XMLElement *pExposureTime = doc.NewElement("ExposureTime");
tinyxml2::XMLText *pExposureTimeText = doc.NewText("200");
pExposureTime->InsertEndChild(pExposureTimeText);
p->InsertEndChild(pExposureTime);
tinyxml2::XMLElement *pTriggerMode = doc.NewElement("TriggerMode");
tinyxml2::XMLText *pTriggerModeText = doc.NewText("false");
pTriggerMode->InsertEndChild(pTriggerModeText);
p->InsertEndChild(pTriggerMode);
}
/**
* @brief: 将数据写到某个xml文件中
* @param path xml文件路径
* @return: 返回说明
* @retval: 返回值说明
*/
static int WriteParam(const std::string &path)
{
tinyxml2::XMLError ret;
tinyxml2::XMLDocument doc;
const char *declaration = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
ret = doc.Parse(declaration);
CHECK_TINYXML2_RESULT(ret);
tinyxml2::XMLElement *pFirst = doc.NewElement("CameraI");
pFirst->SetAttribute("type", "home");
pFirst->SetAttribute("price", 1000);
pFirst->SetAttribute("wifi", true);
// 内部插内容
WriteOneCameraAttibute(pFirst, doc, "1.22.32.82");
doc.InsertEndChild(pFirst);
tinyxml2::XMLElement *pSecond = doc.NewElement("CameraII");
// 内部插内容
WriteOneCameraAttibute(pSecond, doc, "2.22.32.82");
doc.InsertEndChild(pSecond);
tinyxml2::XMLElement *pThree = doc.NewElement("CameraIII");
// 内部插内容
WriteOneCameraAttibute(pThree, doc, "3.22.32.82");
doc.InsertEndChild(pThree);
ret = doc.SaveFile(path.c_str());
return 0;
}
/**
* @brief: 读取每个相机的属性
* @param p 相机节点指针
* @return: 返回说明
* @retval: 返回值说明
*/
static void ReadOneCameraAttribute(const tinyxml2::XMLElement *p)
{
int devIdContent = p->FirstChildElement("DevID")->IntText();
const char *ipAddrContent = p->FirstChildElement("IpAddress")->GetText();
const char *subnetMaskContent = p->FirstChildElement("SubnetMask")->GetText();
const char *exposureAutoContent = p->FirstChildElement("ExposureAuto")->GetText();
int64_t exposureTimeContent = p->FirstChildElement("ExposureTime")->Int64Text();
bool triggerModeContent = p->FirstChildElement("TriggerMode")->BoolText();
std::cout << "devIdContent(int):\t" << devIdContent << std::endl;
std::cout << "ipAddrContent:\t" << ipAddrContent << std::endl;
std::cout << "subnetMaskContent:\t" << subnetMaskContent << std::endl;
std::cout << "exposureAutoContent:\t" << exposureAutoContent << std::endl;
std::cout << "exposureTimeContent(int64_t):\t" << exposureTimeContent << std::endl;
std::cout << "triggerModeContent(bool):\t" << ((triggerModeContent == true) ? "true" : "false") << std::endl;
}
/**
* @brief: 读取解析某路径的xml文件
* @param path xml文件路径
* @return: 返回说明
* @retval: 返回值说明
*/
static int ReadParam(const std::string &path)
{
// 导入文件错误, 退出
tinyxml2::XMLDocument doc;
tinyxml2::XMLError error = doc.LoadFile(path.c_str());
CHECK_TINYXML2_RESULT(error);
tinyxml2::XMLElement *pFirst = doc.FirstChildElement("CameraI"); //doc.RootElement();// 等同
tinyxml2::XMLElement *pSecond = doc.FirstChildElement("CameraII");
tinyxml2::XMLElement *pThree = doc.FirstChildElement("CameraIII");
// 分别读取每个相机的各个属性
ReadOneCameraAttribute(pFirst);
std::cout << "------------------\n";
ReadOneCameraAttribute(pSecond);
std::cout << "------------------\n";
ReadOneCameraAttribute(pThree);
std::cout << "------------------\n";
return 0;
}
/**
* @brief: 修改相机属性
* @param p 相机节点指针
* @return: 返回说明
* @retval: 返回值说明
*/
static void ModifyOneCamera(tinyxml2::XMLElement *p)
{
int devId = 4;
const char *ipAddr = "139.66.38.13";
const char *subnetMask = "255.0.0.0";
bool exposureAuto = false;
int64_t exposureTime = 80;
bool triggerMode = false;
p->FirstChildElement("DevID")->SetText(devId);
p->FirstChildElement("IpAddress")->SetText(ipAddr);
p->FirstChildElement("SubnetMask")->SetText(subnetMask);
p->FirstChildElement("ExposureAuto")->SetText(exposureAuto);
p->FirstChildElement("ExposureTime")->SetText(exposureTime);
p->FirstChildElement("TriggerMode")->SetText(triggerMode);
}
/**
* @brief: 修改某xml文件的参数
* @param path xml文件路径
* @return: 返回说明
* @retval: 返回值说明
*/
static void ModifyParam(const std::string &path)
{
// 导入文件错误, 退出
tinyxml2::XMLDocument doc;
tinyxml2::XMLError error = doc.LoadFile(path.c_str());
if (error != tinyxml2::XMLError::XML_SUCCESS)
return;
// 三个相机指针
tinyxml2::XMLElement *pFirst = doc.FirstChildElement("CameraI");
tinyxml2::XMLElement *pSecond = doc.FirstChildElement("CameraII");
tinyxml2::XMLElement *pThree = doc.FirstChildElement("CameraIII");
// 修改
ModifyOneCamera(pFirst);
ModifyOneCamera(pSecond);
ModifyOneCamera(pThree);
doc.SaveFile(path.c_str());
}
void testCameraXML()
{
std::string path = "camera_info_1.xml";
std::cout << "---------------生成一个xml文件------------------" << std::endl;
WriteParam(path);
std::cout << "--------------写文件结束,读取生成的xml文件------------------" << std::endl;
ReadParam(path);
std::cout << "--------------读文件结束,修改文件开始------------------" << std::endl;
ModifyParam(path);
std::cout << "--------------修改文件结束,读取修改的xml文件------------------" << std::endl;
ReadParam(path);
}
int main(void)
{
testCameraXML();
return 0;
}