Working with XML in C++: Libraries, Parsing, and Creating XML Documents

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Working with XML in C++: An Overview of Libraries for Parsing and Creating XML Documents



XML (Extensible Markup Language) remains a popular format for storing and transmitting structured data, especially in configuration files, web services (SOAP, XML-RPC), and data exchange between systems. C++ has no built-in XML support (except for the outdated MSXML), so developers turn to third-party libraries. In this article, we will look at three of the most popular and lightweight libraries for working with XML in C++: TinyXML-2, PugiXML, and RapidXML. You will learn how to install each of them, how to parse and create XML documents, and we will compare their performance and ease of use.



1. TinyXML-2



TinyXML-2 is a simple, minimalist library consisting of just two files (tinyxml2.h and tinyxml2.cpp). It is ideal for small projects where high performance is not required, but ease of integration is important. The library is licensed under Zlib, allowing its free use in commercial projects.



Installation

Copy the tinyxml2.h and tinyxml2.cpp files from the official repository into your project and include them. No additional dependencies are required.



Example: Reading XML

#include "tinyxml2.h"#include <iostream>

int main() { tinyxml2::XMLDocument doc; doc.LoadFile("config.xml");

tinyxml2::XMLElement* root = doc.RootElement(); if (root) { const char* value = root->FirstChildElement("setting")->GetText(); std::cout << "Setting: " << value << std::endl; } return 0;}


Example: Creating XML

#include "tinyxml2.h"

int main() { tinyxml2::XMLDocument doc; tinyxml2::XMLDeclaration* decl = doc.NewDeclaration(); doc.InsertFirstChild(decl);

tinyxml2::XMLElement* root = doc.NewElement("root"); root->SetAttribute("version", "1.0"); doc.InsertEndChild(root);

tinyxml2::XMLElement* child = doc.NewElement("item"); child->SetText("Hello, XML!"); root->InsertEndChild(child);

doc.SaveFile("output.xml"); return 0;}


2. PugiXML



PugiXML is a faster and more feature-rich library than TinyXML-2. It supports XPath, UTF-8/16 encoding handling, and has a convenient API. PugiXML is often used in projects that require a balance between speed and convenience. It is distributed under the MIT license.



Installation

Download the source code from GitHub and add pugixml.cpp and pugixml.hpp to your project. To use XPath, add PUGIXML_HAS_XPATH to the preprocessor definitions.



Example: Reading XML with XPath

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

int main() { pugi::xml_document doc; doc.load_file("data.xml");

pugi::xpath_node result = doc.select_node("//item[@id='1']"); if (result) { std::cout << "Found: " << result.node().child_value() << std::endl; } return 0;}


Example: Creating XML

#include "pugixml.hpp"

int main() { pugi::xml_document doc; pugi::xml_node root = doc.append_child("configuration"); root.append_attribute("version") = "2.0";

pugi::xml_node param = root.append_child("param"); param.append_attribute("name") = "timeout"; param.text().set("30");

doc.save_file("config.xml"); return 0;}


3. RapidXML



RapidXML is the fastest library for parsing XML in C++. It works in-place (modifies the source

Recommendations