rapidxml的使用方式

Posted by Roy Zhang on 2019-08-01

RapidXml is an attempt to create the fastest XML parser possible, while retaining useability, portability and reasonable W3C compatibility. It is an in-situ parser written in modern C++, with parsing speed approaching that of strlen function executed on the same data.

RapidXml has been around since 2006, and is being used by lots of people. HTC uses it in some of its mobile phones.

If you are looking for a stable and fast parser, look no further. Integration with your project will be trivial, because entire library is contained in a single header file, and requires no building or configuration.

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
void RouteNode::route2xml(Route route)
{
xml_document<> doc;
xml_node<> *rot = doc.allocate_node(rapidxml::node_pi, doc.allocate_string("xml version='1.0' encoding='utf-8'"));
doc.append_node(rot);

xml_node<> *node = doc.allocate_node(node_element, "route", "imformation");
node->append_attribute(doc.allocate_attribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance"));
node->append_attribute(doc.allocate_attribute("xmlns", "http://www.cirm.org/RTZ/1/1"));
node->append_attribute(doc.allocate_attribute("version", "1.1"));
node->append_attribute(doc.allocate_attribute("xmlns:stm", "http://stmvalidation.eu/STM/1/0/0"));
node->append_attribute(doc.allocate_attribute("xsi:schemaLocation", "http://stmvalidation.eu/STM/1/0/0 stm_extensions.xsd"));
doc.append_node(node);
/***route info**/
xml_node<> *routeInfo = doc.allocate_node(node_element, "routeInfo", NULL);
routeInfo->append_attribute(doc.allocate_attribute("routeName", "Example_routes"));
// routeInfo->append_attribute(doc.allocate_attribute("vesselVoyage", " "));
// routeInfo->append_attribute(doc.allocate_attribute("routeStatus", " "));
node->append_node(routeInfo);
/**extensions**/
// xml_node<> *extensions = doc.allocate_node(node_element, "extensions", "imformation");
// routeInfo->append_node(extensions);
// xml_node<> *extension = doc.allocate_node(node_element, "extension", NULL);
// extension->append_attribute(doc.allocate_attribute("name", "routeInfoEx"));
// extension->append_attribute(doc.allocate_attribute("xsi:type", "stm:RouteInfoExtension"));
// extension->append_attribute(doc.allocate_attribute("routeStatusEnum", "1"));
// extension->append_attribute(doc.allocate_attribute("manufacturer", "STM"));
// extension->append_attribute(doc.allocate_attribute("version", "1.0.0"));
// extensions->append_node(extension);
/********/

/***waypoints***/
xml_node<> *waypoints = doc.allocate_node(node_element, "waypoints", "imformation");
node->append_node(waypoints);
/***waypoint***/

for (int i = 0; i < route.m_waypoints.size(); i++)
{
std::string id = std::to_string(route.m_waypoints[i].m_id);
char *idc = doc.allocate_string(id.c_str());

std::string radius = std::to_string(route.m_waypoints[i].m_radius);
char *radiusc = doc.allocate_string(radius.c_str());
std::string latitude = std::to_string(route.m_waypoints[i].m_latitude);
char *latitudec = doc.allocate_string(latitude.c_str());
std::string longitude = std::to_string(route.m_waypoints[i].m_longitude);
char *longitudec = doc.allocate_string(longitude.c_str());

xml_node<> *waypoint = doc.allocate_node(node_element, "waypoint", "imformation");
waypoint->append_attribute(doc.allocate_attribute("id", idc));
waypoint->append_attribute(doc.allocate_attribute("radius", radiusc));
waypoints->append_node(waypoint);

xml_node<> *position = doc.allocate_node(node_element, "position", NULL);
position->append_attribute(doc.allocate_attribute("lat", latitudec));
position->append_attribute(doc.allocate_attribute("lon", longitudec));

xml_node<> *leg = doc.allocate_node(node_element, "leg", NULL);
leg->append_attribute(doc.allocate_attribute("starboardXTD", "0.026998"));
leg->append_attribute(doc.allocate_attribute("portsideXTD", "0.026998"));
leg->append_attribute(doc.allocate_attribute("geometryType", "Loxodrome"));
waypoint->append_node(position);
waypoint->append_node(leg);
}
xml_node<> *schedules = doc.allocate_node(node_element, "schedules", "imformation");
node->append_node(schedules);
xml_node<> *schedule = doc.allocate_node(node_element, "schedule", "imformation");
schedule->append_attribute(doc.allocate_attribute("id", "1"));
schedules->append_node(schedule);
xml_node<> *manual = doc.allocate_node(node_element, "manual", "imformation");
schedule->append_node(manual);
for (int i = 0; i < route.m_waypoints.size(); i++)
{
if (i != route.m_waypoints.size() - 1)
{
std::string id = std::to_string(route.m_waypoints[i].m_id);
char *idc = doc.allocate_string(id.c_str());

std::string speed = std::to_string(route.m_waypoints[i].m_speed);
char *speedc = doc.allocate_string(speed.c_str());

xml_node<> *scheduleElement = doc.allocate_node(node_element, "scheduleElement", NULL);
scheduleElement->append_attribute(doc.allocate_attribute("waypointId", idc));
scheduleElement->append_attribute(doc.allocate_attribute("speed", speedc));
manual->append_node(scheduleElement);
}
else
{
std::string id = std::to_string(route.m_waypoints[i].m_id);
char *idc = doc.allocate_string(id.c_str());

std::string speed = std::to_string(route.m_waypoints[i].m_speed);
char *speedc = doc.allocate_string(speed.c_str());

xml_node<> *scheduleElement = doc.allocate_node(node_element, "scheduleElement", NULL);
scheduleElement->append_attribute(doc.allocate_attribute("waypointId", idc));
scheduleElement->append_attribute(doc.allocate_attribute("eta", "2019-06-17T12:28:24+02:00"));
scheduleElement->append_attribute(doc.allocate_attribute("speed", speedc));
manual->append_node(scheduleElement);
}
}
xml_node<> *calculated = doc.allocate_node(node_element, "calculated", "imformation");
schedule->append_node(calculated);
for (int i = 0; i < route.m_waypoints.size(); i++)
{
if (i == 0)
{
std::string id = std::to_string(route.m_waypoints[i].m_id);
char *idc = doc.allocate_string(id.c_str());

std::string speed = std::to_string(route.m_waypoints[i].m_speed);
char *speedc = doc.allocate_string(speed.c_str());

xml_node<> *scheduleElement = doc.allocate_node(node_element, "scheduleElement", NULL);
scheduleElement->append_attribute(doc.allocate_attribute("waypointId", idc));
scheduleElement->append_attribute(doc.allocate_attribute("etd", "2019-06-17T12:22:31+02:00"));
scheduleElement->append_attribute(doc.allocate_attribute("speed", speedc));
calculated->append_node(scheduleElement);
}
else if (i == route.m_waypoints.size() - 1)
{
std::string id = std::to_string(route.m_waypoints[i].m_id);
char *idc = doc.allocate_string(id.c_str());

std::string speed = std::to_string(route.m_waypoints[i].m_speed);
char *speedc = doc.allocate_string(speed.c_str());

xml_node<> *scheduleElement = doc.allocate_node(node_element, "scheduleElement", NULL);
scheduleElement->append_attribute(doc.allocate_attribute("waypointId", idc));
scheduleElement->append_attribute(doc.allocate_attribute("eta", "2019-06-17T12:28:24+02:00"));
scheduleElement->append_attribute(doc.allocate_attribute("speed", speedc));
calculated->append_node(scheduleElement);
}
else
{
std::string id = std::to_string(route.m_waypoints[i].m_id);
char *idc = doc.allocate_string(id.c_str());

std::string speed = std::to_string(route.m_waypoints[i].m_speed);
char *speedc = doc.allocate_string(speed.c_str());

xml_node<> *scheduleElement = doc.allocate_node(node_element, "scheduleElement", NULL);
scheduleElement->append_attribute(doc.allocate_attribute("waypointId", idc));
scheduleElement->append_attribute(doc.allocate_attribute("speed", speedc));
calculated->append_node(scheduleElement);
}
}
/**extensions_end**/
// xml_node<> *extensions_end = doc.allocate_node(node_element, "extensions", "imformation");
// node->append_node(extensions_end);
// xml_node<> *extension_end = doc.allocate_node(node_element, "extensions", NULL);
// extensions_end->append_node(extension_end);
// extension_end->append_attribute(doc.allocate_attribute("manufacturer", "transas"));
// extension_end->append_attribute(doc.allocate_attribute("version", "1.0"));
// extension_end->append_attribute(doc.allocate_attribute("name", "product_info"));
// extension_end->append_attribute(doc.allocate_attribute("direction", "Wärtsilä iSailor"));
// extension_end->append_attribute(doc.allocate_attribute("solution_version", "1.11.0 (9169)"));
/**extention_end**/

std::string text;
rapidxml::print(std::back_inserter(text), doc, 0);

std::cout << text << std::endl;

std::ofstream out("config11.xml");
out << doc;
}