File: xml.md

package info (click to toggle)
reflect-cpp 0.18.0%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 12,524 kB
  • sloc: cpp: 44,484; python: 131; makefile: 30; sh: 3
file content (248 lines) | stat: -rw-r--r-- 8,509 bytes parent folder | download | duplicates (2)
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# XML 

For XML support, you must also include the header `<rfl/xml.hpp>` and include the [pugixml](https://github.com/zeux/pugixml) library.
Furthermore, when compiling reflect-cpp, you need to pass `-DREFLECTCPP_XML=ON` to cmake. If you are using vcpkg or Conan, there
should be an appropriate feature (vcpkg) or option (Conan) that will abstract this away for you.


## Simple example

Consider the following example:

```cpp
using Age = rfl::Validator<unsigned int, rfl::Minimum<0>, rfl::Maximum<130>>;

struct Person {
  rfl::Rename<"firstName", std::string> first_name;
  rfl::Rename<"lastName", std::string> last_name = "Simpson";
  std::string town = "Springfield";
  rfl::Timestamp<"%Y-%m-%d"> birthday;
  Age age;
  rfl::Email email;
  std::vector<Person> child;
};

const auto bart = Person{.first_name = "Bart",
                       .birthday = "1987-04-19",
                       .age = 10,
                       .email = "bart@simpson.com"};

const auto lisa = Person{.first_name = "Lisa",
                       .birthday = "1987-04-19",
                       .age = 8,
                       .email = "lisa@simpson.com"};

const auto maggie = Person{.first_name = "Maggie",
                         .birthday = "1987-04-19",
                         .age = 0,
                         .email = "maggie@simpson.com"};

const auto homer = Person{.first_name = "Homer",
                        .birthday = "1987-04-19",
                        .age = 45,
                        .email = "homer@simpson.com",
                        .child = std::vector<Person>({bart, lisa, maggie})};

rfl::xml::write(homer);
```

This will result in the following XML string:

```xml
<?xml version="1.0" encoding="UTF-8"?>
<Person>
    <firstName>Homer</firstName>
    <lastName>Simpson</lastName>
    <town>Springfield</town>
    <birthday>1987-04-19</birthday>
    <age>45</age>
    <email>homer@simpson.com</email>
    <child>
        <firstName>Bart</firstName>
        <lastName>Simpson</lastName>
        <town>Springfield</town>
        <birthday>1987-04-19</birthday>
        <age>10</age>
        <email>bart@simpson.com</email>
    </child>
    <child>
        <firstName>Lisa</firstName>
        <lastName>Simpson</lastName>
        <town>Springfield</town>
        <birthday>1987-04-19</birthday>
        <age>8</age>
        <email>lisa@simpson.com</email>
    </child>
    <child>
        <firstName>Maggie</firstName>
        <lastName>Simpson</lastName>
        <town>Springfield</town>
        <birthday>1987-04-19</birthday>
        <age>0</age>
        <email>maggie@simpson.com</email>
    </child>
</Person>
```

Unlike most other formats, XML distinguishes between attributes and nodes. 
If you want something to be displayed as an attribute, you must mark it as such:

```cpp
using Age = rfl::Validator<unsigned int, rfl::Minimum<0>, rfl::Maximum<130>>;

struct Person {
  rfl::Rename<"firstName", rfl::Attribute<std::string>> first_name;
  rfl::Rename<"lastName", rfl::Attribute<std::string>> last_name = "Simpson";
  rfl::Attribute<std::string> town = "Springfield";
  rfl::Attribute<rfl::Timestamp<"%Y-%m-%d">> birthday;
  rfl::Attribute<Age> age;
  rfl::Attribute<rfl::Email> email;
  std::vector<Person> child;
};

const auto bart = Person{.first_name = "Bart",
                         .birthday = "1987-04-19",
                         .age = 10,
                         .email = "bart@simpson.com"};

const auto lisa = Person{.first_name = "Lisa",
                         .birthday = "1987-04-19",
                         .age = 8,
                         .email = "lisa@simpson.com"};

const auto maggie = Person{.first_name = "Maggie",
                           .birthday = "1987-04-19",
                           .age = 0,
                           .email = "maggie@simpson.com"};

const auto homer = Person{.first_name = "Homer",
                          .birthday = "1987-04-19",
                          .age = 45,
                          .email = "homer@simpson.com",
                          .child = std::vector<Person>({bart, lisa, maggie})};

rfl::xml::write(homer);
```

This will result in the following XML string:

```xml
<?xml version="1.0" encoding="UTF-8"?>
<Person firstName="Homer" lastName="Simpson" town="Springfield" birthday="1987-04-19" age="45" email="homer@simpson.com">
    <child firstName="Bart" lastName="Simpson" town="Springfield" birthday="1987-04-19" age="10" email="bart@simpson.com" />
    <child firstName="Lisa" lastName="Simpson" town="Springfield" birthday="1987-04-19" age="8" email="lisa@simpson.com" />
    <child firstName="Maggie" lastName="Simpson" town="Springfield" birthday="1987-04-19" age="0" email="maggie@simpson.com" />
</Person>
```

Note that only boolean values, string values, integral values or floating point values can be represented as attributes.

There also is a special field name called `xml_content` to be used when you want a value directly inserted into the content.
Again, only boolean values, string values, integral values or floating point values can be represented this way:

```cpp
struct Person {
  std::string xml_content;
  rfl::Attribute<std::string> town = "Springfield";
  rfl::Attribute<rfl::Timestamp<"%Y-%m-%d">> birthday;
  rfl::Attribute<rfl::Email> email;
  std::vector<Person> child;
};

const auto bart = Person{.xml_content = "Bart Simpson",
                         .birthday = "1987-04-19",
                         .email = "bart@simpson.com"};

const auto lisa = Person{.xml_content = "Lisa Simpson",
                         .birthday = "1987-04-19",
                         .email = "lisa@simpson.com"};

const auto maggie = Person{.xml_content = "Maggie Simpson",
                           .birthday = "1987-04-19",
                           .email = "maggie@simpson.com"};

const auto homer = Person{.xml_content = "Homer Simpson",
                          .birthday = "1987-04-19",
                          .email = "homer@simpson.com",
                          .child = std::vector<Person>({bart, lisa, maggie})};

rfl::xml::write(homer);
```

This will result in the following XML string:

```xml
<?xml version="1.0" encoding="UTF-8"?>
<Person town="Springfield" birthday="1987-04-19" email="homer@simpson.com">Homer Simpson<child town="Springfield" birthday="1987-04-19" email="bart@simpson.com">Bart Simpson</child>
    <child town="Springfield" birthday="1987-04-19" email="lisa@simpson.com">Lisa Simpson</child>
    <child town="Springfield" birthday="1987-04-19" email="maggie@simpson.com">Maggie Simpson</child>
</Person>
```

## Limitations of the XML format

### There must be exactly one root node

The XML format requires that there must be exactly one root node. 
Other formats, like JSON, allow you to have an array as the root element. XML disallows that.

### The root node must have a name

As you may have noticed, reflect-cpp uses the names of fields as the names of the XML nodes or attributes.
However, the root node does not have a field name associated with it. So reflect-cpp will use the name
of the struct as the name of the root node. This is a problem when the root node is a template parameter,
because the name of the root node cannot contain "<" or ">".

In this case, you can explicitly set a root node name like this:

```cpp
rfl::xml::write<"NameOfTheRootNode">(homer);
```

### No nested arrays

Unlike JSON, XML doesn't have an explicit concept of arrays. Array-like structures are represented by simply
writing the same field over and over again (i.e. Homer's children in the examples above).

This also implies that something like this cannot be properly represented in XML:

```cpp
struct Person {
  std::string name;
  std::vector<std::vector<Person>> child;
};
```

There is no way to represent a vector of vectors in XML. It's a limitation of the format.

### Integers cannot be tags

In JSON, it is possible to represent a map with integers as keys like this:

```cpp
std::map<int, std::string> homer;
homer[1] = "Homer";
homer[2] = "Simpson";

rfl::json::write(homer);
```

This will be represented as follows:

```json
{"1":"Homer","2":"Simpson"}
```

In XML, this is impossible. The XML standard requires that the tag must not start with an integer.

## Loading and saving

You can also load and save to disc using a very similar syntax:

```cpp
const rfl::Result<Person> result = rfl::xml::load<Person>("/path/to/file.xml");

const auto person = Person{...};
rfl::xml::save("/path/to/file.xml", person);
```