# XML For XML support, you must also include the header `` 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, 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 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({bart, lisa, maggie})}; rfl::xml::write(homer); ``` This will result in the following XML string: ```xml Homer Simpson Springfield 1987-04-19 45 homer@simpson.com Bart Simpson Springfield 1987-04-19 10 bart@simpson.com Lisa Simpson Springfield 1987-04-19 8 lisa@simpson.com Maggie Simpson Springfield 1987-04-19 0 maggie@simpson.com ``` 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, rfl::Maximum<130>>; struct Person { rfl::Rename<"firstName", rfl::Attribute> first_name; rfl::Rename<"lastName", rfl::Attribute> last_name = "Simpson"; rfl::Attribute town = "Springfield"; rfl::Attribute> birthday; rfl::Attribute age; rfl::Attribute email; std::vector 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({bart, lisa, maggie})}; rfl::xml::write(homer); ``` This will result in the following XML string: ```xml ``` 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 town = "Springfield"; rfl::Attribute> birthday; rfl::Attribute email; std::vector 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({bart, lisa, maggie})}; rfl::xml::write(homer); ``` This will result in the following XML string: ```xml Homer SimpsonBart Simpson Lisa Simpson Maggie Simpson ``` ## 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> 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 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 result = rfl::xml::load("/path/to/file.xml"); const auto person = Person{...}; rfl::xml::save("/path/to/file.xml", person); ```