A lightweight, header-only XML parsing and generation library for C++.
Xstream is a simple XML parser and generator that provides a clean interface for working with XML data in C++ applications. It offers both reading (parsing) and writing (generating) capabilities with minimal dependencies.
- Header-only library - just include
xstream.h
and start using it - XML parsing with support for elements, attributes, CDATA, and comments
- XML generation with proper indentation and formatting
- HTML entity encoding and decoding
- Path-based element navigation
- Modern C++ design using C++17 features (string_view, optional)
- No external dependencies
#include "xstream.h"
void parse_example() {
std::string xml = "<root><item id='1'>Hello, <![CDATA[world]]></item></root>";
xstream::Reader reader(xml);
while (reader.next()) {
if (reader.is_start_element()) {
std::string name = reader.name();
printf("Start element: %s\n", name.c_str());
// Access attributes
auto id_attr = reader.attribute("id");
if (id_attr) {
printf(" Attribute id: %s\n", std::string(*id_attr).c_str());
}
}
else if (reader.is_end_element()) {
printf("End element: %s\n", reader.name().c_str());
printf(" Text content: %s\n", reader.text().c_str());
}
else if (reader.is_characters()) {
auto chars = reader.characters().decode();
printf("Characters: %s\n", std::string(chars.data(), chars.size()).c_str());
}
}
}
#include "xstream.h"
#include <string>
#include <vector>
void write_example() {
std::vector<char> output;
auto writer_fn = [&output](const char* data, int size) -> int {
output.insert(output.end(), data, data + size);
return size;
};
xstream::Writer writer(writer_fn);
writer.start_document();
writer.start_element("root");
writer.start_element("item");
writer.write_attribute("id", "1");
writer.write_characters("Hello, world!");
writer.end_element(); // Close item
// Alternative syntax using lambda
writer.element("item", [&](){
writer.write_attribute("id", "2");
writer.write_characters("Another item");
});
// Simple text element
writer.text_element("simple", "Text content");
writer.end_element(); // Close root
writer.end_document();
// output vector now contains the formatted XML
std::string result(output.begin(), output.end());
printf("%s\n", result.c_str());
}
The xstream::Reader
class provides the following key methods:
Reader(string_view)
: Constructor that takes XML datanext()
: Move to the next element, returns false when donestate()
: Get current state (StartElement, EndElement, Characters, etc.)is_start_element()
,is_end_element()
,is_characters()
: Type checkingname()
: Get current element nametext()
: Get text content of the current elementattribute(name)
: Get an attribute value by nameattributes()
: Get all attributespath()
: Get current element path
The xstream::Writer
class provides:
Writer(function)
: Constructor that takes a writer functionstart_document()
,end_document()
: Document structurestart_element(name)
,end_element()
: Element handlingwrite_attribute(name, value)
: Add an attributewrite_characters(text)
: Add text contentelement(name, function)
: Create element with lambda for contenttext_element(name, text)
: Create element with simple text content
The project uses the Qt build system with .pro
files, but the library itself has no dependencies on Qt.
To build the test program:
cd test
qmake
make
This project is provided as-is with no warranty. Use at your own risk.
- C++17 compatible compiler
- No external dependencies for the library itself