FormatterHQ

XML to C++ Class Generator

Generate C++ classes from XML: members with getters and setters for attributes and elements, std::vector for repeated elements and std::optional where values vary.

Input XML
Output CPP
Ready.

How to use the XML to C++

  1. Paste your XML document into the Input editor, or use Sample, Upload file or Load URL, or press Sample to see an example.
  2. Leave Top-level class name empty to name the root type after the root element, or type your own name.
  3. Tick Types only (no helpers) for the classes alone, or leave it off to include nlohmann/json conversion code for the JSON form.
  4. Press Generate (it also updates as you edit), then use Copy or Download.

XML to C++ data classes

C++ XML libraries such as pugixml, tinyxml2 and RapidXML are DOM or SAX parsers: they give you nodes and attribute strings, not typed objects. This XML to C++ generator writes the typed classes from a sample document, with private members, const and mutable getters, setters, std::vector for repeated elements and std::optional where a value is not always present.

Example: a 3D scene description

<scene name="level1">
  <camera fov="60" x="0" y="2.5" z="-10"/>
  <mesh file="tree.obj" x="4" y="0" z="3"/>
  <mesh file="rock.obj" x="-2" y="0" z="7"/>
</scene>
class Camera {
    std::optional<int64_t> fov;
    int64_t x;
    double y;
    int64_t z;
    std::optional<std::string> file;
    // getters and setters
};

class Scene {
    Camera camera;
    std::vector<Camera> mesh;
    std::string name;
    // getters and setters
};

Notice that <camera> and <mesh> were merged into one class. They share the x, y and z attributes, so the type inference treated them as the same shape and made the attributes that only one of them has (fov, file) optional. Split them into two classes by hand if they are different concepts in your program. Also, y became double because one value was 2.5, while x and z only had whole numbers; make all coordinates double.

Filling the classes with pugixml

pugi::xml_document doc;
doc.load_string(xml.c_str());
auto sceneNode = doc.child("scene");

Scene scene;
scene.set_name(sceneNode.attribute("name").as_string());
for (auto m : sceneNode.children("mesh")) {
    Camera mesh;
    mesh.set_file(std::string(m.attribute("file").as_string()));
    mesh.set_x(m.attribute("x").as_llong());
    scene.get_mutable_mesh().push_back(mesh);
}

pugixml's as_int, as_llong, as_double and as_bool convert attribute text, and child_value() reads element text (the generator's value member). With tinyxml2 the equivalent calls are Attribute(), QueryIntAttribute() and GetText().

DOM or SAX?

For configuration files and scene descriptions like this one, a DOM parser such as pugixml is simplest: load the document, walk the nodes, fill the classes. For very large files, an event-based (SAX-style) parser such as Expat keeps memory flat; you create a Camera object on each start tag, set members from the attribute array, and push it into the scene when the element ends. Either way, the generated classes give the parsing code a clear, typed destination.

Notes

  • The full-mode helpers target nlohmann/json and read the JSON form of the data, not XML.
  • Include two or more of each repeated element so it becomes a std::vector.
  • std::optional requires C++17.

Format the classes with the C++ Formatter. For JSON sources, see JSON to C++. Check the XML with the XML Validator. Generation runs in your browser.

Frequently asked questions

Why were two different elements merged into one class?
When element structures share most of their properties, type inference treats them as the same type and makes the differing properties optional. Split the class manually if the elements mean different things.
Which XML library should I use with the classes?
Any DOM parser works. pugixml and tinyxml2 are popular, lightweight choices; read attributes and text with their accessors and call the generated setters.
Why is one coordinate double and the others int64_t?
Types are inferred from the sample values. Only y had a decimal, so only y became double. Adjust related fields to the same type.
Do the generated helpers parse XML?
No. The optional helpers use nlohmann/json and work with the JSON representation of the data.

Last updated September 26, 2026

Related tools

JSON to C++ Generate C++ classes with nlohmann::json helpers from a JSON sample instantly, with nested types detected automatically. XML to Crystal Generate Crystal classes with JSON mapping from a XML sample instantly, with nested types detected automatically. XML to C# Generate C# classes with JsonProperty attributes from a XML sample instantly, with nested types detected automatically. XML to Dart Generate Dart classes with fromJson and toJson from a XML sample instantly, with nested types detected automatically. XML to Elm Generate Elm type aliases with decoders from a XML sample instantly, with nested types detected automatically. XML to Flow Generate Flow Flow type definitions from a XML sample instantly, with nested types detected automatically. XML to Go Generate Go structs with json tags from a XML sample instantly, with nested types detected automatically. XML to Haskell Generate Haskell records with Aeson instances from a XML sample instantly, with nested types detected automatically. XML to Java Generate Java POJO classes with getters and setters from a XML sample instantly, with nested types detected automatically. XML to JavaScript PropTypes Generate JavaScript PropTypes React PropTypes definitions from a XML sample instantly, with nested types detected automatically. XML to JSON Schema Generate JSON Schema a JSON Schema document from a XML sample instantly, with nested types detected automatically. XML to Kotlin Generate Kotlin data classes from a XML sample instantly, with nested types detected automatically.