Implementing XML serialization and deserialization in C++

In this blog post, we will explore how to implement XML serialization and deserialization in C++. XML (eXtensible Markup Language) is a popular data format that is widely used for data storage and exchange due to its platform-neutral and human-readable nature. Serializing and deserializing objects to and from XML format is a common requirement in many C++ applications. Let’s dive in and see how it can be done!

XML Serialization

XML serialization is the process of converting objects from their native data structures to an XML representation. This allows the objects to be persisted or transmitted in XML format. In C++, we can accomplish XML serialization using various libraries such as boost::serialization or pugixml.

Here’s an example demonstrating XML serialization using the pugixml library:

#include <iostream>
#include "pugixml.hpp"

struct Person {
    std::string name;
    int age;
};

void serializeXML(const Person& person) {
    pugi::xml_document doc;
    pugi::xml_node rootNode = doc.append_child("Person");

    pugi::xml_node nameNode = rootNode.append_child("Name");
    nameNode.text().set(person.name.c_str());

    pugi::xml_node ageNode = rootNode.append_child("Age");
    ageNode.text().set(std::to_string(person.age).c_str());

    doc.save_file("person.xml");
}

int main() {
    Person person;
    person.name = "John Doe";
    person.age = 30;

    serializeXML(person);

    return 0;
}

In this example, we define a Person struct with name and age fields. The serializeXML function takes a Person object and creates an XML document using the pugixml library. The XML document is then saved to a file named “person.xml”.

XML Deserialization

XML deserialization is the reverse process of XML serialization. It involves converting XML data back into its original object representation. Similar to XML serialization, we can use libraries like boost::serialization or pugixml for XML deserialization in C++.

Let’s see an example of XML deserialization using pugixml:

#include <iostream>
#include "pugixml.hpp"

struct Person {
    std::string name;
    int age;
};

Person deserializeXML() {
    Person person;

    pugi::xml_document doc;
    if (doc.load_file("person.xml")) {
        pugi::xml_node rootNode = doc.child("Person");

        pugi::xml_node nameNode = rootNode.child("Name");
        person.name = nameNode.text().get();

        pugi::xml_node ageNode = rootNode.child("Age");
        person.age = std::stoi(ageNode.text().get());
    }

    return person;
}

int main() {
    Person person = deserializeXML();

    std::cout << "Name: " << person.name << std::endl;
    std::cout << "Age: " << person.age << std::endl;

    return 0;
}

In this example, the deserializeXML function loads the XML file “person.xml” using pugixml and extracts the name and age nodes from the XML data. It then assigns these values to the corresponding fields of a Person object, which is returned.

Conclusion

XML serialization and deserialization are essential techniques for working with XML data in C++. By using libraries like pugixml, we can easily convert objects to XML format and vice versa. This capability allows us to store, transmit, and transfer data in a human-readable and platform-independent manner. Start using XML serialization and deserialization to enhance the functionality of your C++ applications today!

#cpp #XML #serialization #deserialization