Working with XML in C++: PugiXML and RapidXML Libraries
XML (Extensible Markup Language) remains one of the key formats for storing and transmitting structured data. In C++, there are several powerful libraries for working with XML, among which PugiXML (lightweight, with XPath support) and RapidXML (extremely fast, working "in-place") stand out. In this article, we will cover both libraries, their features, installation, and practical examples.
1. PugiXML — A Versatile Tool
PugiXML is a lightweight, cross-platform library with support for XPath 1.0 and UTF-8/UTF-16 encodings. It is ideal for most tasks: from reading configuration files to complex analysis of XML documents.
Installation
Download the header files from the official website or use a package manager (vcpkg, Conan). Include in your project:
#include "pugixml.hpp"using namespace pugi;Example: Loading and Reading XML
#include <iostream>#include "pugixml.hpp"
int main() { xml_document doc; if (!doc.load_file("config.xml")) { std::cerr << "Error loading file"; return -1; }
// Get the root element xml_node root = doc.child("settings"); for (xml_node node = root.child("param"); node; node = node.next_sibling("param")) { std::string name = node.attribute("name").as_string(); int value = node.attribute("value").as_int(); std::cout << "Parameter: " << name << " = " << value << ""; } return 0;}Example: Creating an XML Document
xml_document doc;xml_node root = doc.append_child("catalog");for (int i = 1; i <= 3; ++i) { xml_node item = root.append_child("item"); item.append_attribute("id") = i; item.append_child("name").text() = ("Product " + std::to_string(i)).c_str(); item.append_child("price").text() = 100.0 * i;}doc.save_file("catalog.xml");Advantages of PugiXML:
- XPath support for complex queries (e.g.,
doc.select_nodes("//item[@id>1]")). - Encoding and Unicode support.
- Detailed documentation and an active community.
2. RapidXML — Speed and Minimalism
RapidXML is a header-only library that parses XML without copying data (in-place). It works significantly faster than PugiXML, but does not support XPath and requires caution with string lifetimes.
Installation
Copy rapidxml.hpp and additional files (rapidxml_utils.hpp, rapidxml_print.hpp) into your project.
Example: Parsing a String
#include <iostream>#include "rapidxml.hpp"
int main() { char xml[] = "<root><item id='1'>Value</item></root>"; rapidxml::xml_document<> doc; doc.parse<0>(xml); // 0 — parsing flags
rapidxml::xml_node<>* root = doc.first_node("root"); rapidxml::xml_node<>* item = root->first_node("item"); if (item) { std::cout << "ID: " << item->first_attribute("id")->value() << ""; std::cout << "Content: " << item->value() << ""; } return 0;}Example: Creating XML
rapidxml::xml_document<> doc;rapidxml::xml_node<>* root = doc.allocate_node(rapidxml::node_element, "data");doc.append_node(root);
rapidxml::xml_node<>* child = doc.allocate_node(rapidxml::node_element, "value", "42");root->append_node(child);
// Output to stringstd::string result;rapidxml::print(std::back_inserter(result), doc);std::cout << result;Important: