Xml in C++

Here’s the translation of the XML example from Go to C++:

#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <memory>
#include <pugixml.hpp>

// Plant will be mapped to XML. We'll use pugixml library for XML handling in C++.
class Plant {
public:
    int id;
    std::string name;
    std::vector<std::string> origin;

    Plant(int id, const std::string& name) : id(id), name(name) {}

    std::string toString() const {
        std::ostringstream oss;
        oss << "Plant id=" << id << ", name=" << name << ", origin=[";
        for (size_t i = 0; i < origin.size(); ++i) {
            if (i > 0) oss << " ";
            oss << origin[i];
        }
        oss << "]";
        return oss.str();
    }
};

int main() {
    auto coffee = std::make_shared<Plant>(27, "Coffee");
    coffee->origin = {"Ethiopia", "Brazil"};

    // Create an XML document
    pugi::xml_document doc;
    pugi::xml_node plant = doc.append_child("plant");
    plant.append_attribute("id") = coffee->id;
    plant.append_child("name").text().set(coffee->name.c_str());
    for (const auto& origin : coffee->origin) {
        plant.append_child("origin").text().set(origin.c_str());
    }

    // Save XML to string with indentation
    std::ostringstream oss;
    doc.save(oss, "  ");
    std::cout << oss.str() << std::endl;

    // To add a generic XML header to the output, prepend it explicitly
    std::cout << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" << oss.str() << std::endl;

    // Parse XML back into a Plant object
    pugi::xml_document doc2;
    if (doc2.load_string(oss.str().c_str())) {
        pugi::xml_node plant_node = doc2.child("plant");
        Plant p(plant_node.attribute("id").as_int(), plant_node.child_value("name"));
        for (pugi::xml_node origin : plant_node.children("origin")) {
            p.origin.push_back(origin.child_value());
        }
        std::cout << p.toString() << std::endl;
    }

    auto tomato = std::make_shared<Plant>(81, "Tomato");
    tomato->origin = {"Mexico", "California"};

    // The parent>child>plant structure in XML
    pugi::xml_document nesting_doc;
    pugi::xml_node nesting = nesting_doc.append_child("nesting");
    pugi::xml_node parent = nesting.append_child("parent");
    pugi::xml_node child = parent.append_child("child");

    std::vector<std::shared_ptr<Plant>> plants = {coffee, tomato};
    for (const auto& p : plants) {
        pugi::xml_node plant_node = child.append_child("plant");
        plant_node.append_attribute("id") = p->id;
        plant_node.append_child("name").text().set(p->name.c_str());
        for (const auto& origin : p->origin) {
            plant_node.append_child("origin").text().set(origin.c_str());
        }
    }

    // Save nested XML to string with indentation
    std::ostringstream nested_oss;
    nesting_doc.save(nested_oss, "  ");
    std::cout << nested_oss.str() << std::endl;

    return 0;
}

This C++ code demonstrates XML handling using the pugixml library, which is a lightweight XML processing library for C++. The code structure and functionality closely mirror the original Go example:

  1. We define a Plant class that represents the structure we want to serialize to and deserialize from XML.

  2. In the main function, we create Plant objects and demonstrate XML creation, serialization, and deserialization.

  3. We use pugixml to create XML structures, including nested structures.

  4. The code shows how to add XML attributes, child elements, and how to handle collections (like the origin vector).

  5. We demonstrate pretty-printing XML with indentation.

  6. The example includes parsing XML back into C++ objects.

  7. Finally, we show how to create more complex nested XML structures.

Note that to run this code, you’ll need to install and link against the pugixml library. Also, error handling has been simplified for brevity. In a production environment, you’d want to add more robust error checking and handling.

The output of this program will be similar to the Go version, with XML representations of the Plant objects and the nested structure.