Minimal XML Parser for Embedded Systems
SparseXML is a lightweight XML parser designed for resource-constrained embedded systems. It provides SAX-style event-driven parsing with minimal memory footprint (~1KB) and zero external dependencies.
Simply copy these 3 files to your project:
sparsexml.h– Public API headersparsexml-priv.h– Private definitionssparsexml.c– Implementation
Then include sparsexml.h in your code. No build system or external dependencies required.
- Minimal Memory: Fixed 1KB buffer, no dynamic allocation
- Event-Driven: SAX-style callbacks for tags, attributes, content
- Entity Processing: Standard XML, numeric, and HTML entities
- EXI Support: Schema-less EXI binary format parsing
- Embedded-Ready: Pure C, zero dependencies
#include "sparsexml.h"
unsigned char on_tag(char* name) {
printf("Tag: %s\n", name);
return SXMLExplorerContinue;
}
unsigned char on_content(char* content) {
printf("Content: %s\n", content);
return SXMLExplorerContinue;
}
int main() {
SXMLExplorer* ex = sxml_make_explorer();
sxml_register_func(ex, on_tag, on_content, NULL, NULL);
sxml_enable_entity_processing(ex, 1);
char xml[] = "<root>Hello & World</root>";
sxml_run_explorer(ex, xml);
sxml_destroy_explorer(ex);
return 0;
}make
./test-sparsexml
./examples/simple- RAM: ~1KB for parsing buffer + minimal stack usage
- Flash: ~2KB for code (varies by compiler/architecture)
- ✅ Basic XML structure parsing
- ✅ XML comments (
<!-- -->) - ✅ CDATA sections (
<![CDATA[]]>) - ✅ Standard XML entities (
<,>,&,",') - ✅ Numeric character references (
A,A) - ✅ Extended HTML entities (
©,®,™, , etc.) - ✅ XML namespaces (
ns:tagformat) - ✅ Basic DOCTYPE parsing
- ✅ Buffer overflow protection
- ✅ Configurable entity processing
Parse W3C EXI binary files in schema-less mode:
// Parse EXI binary data
unsigned char result = sxml_run_explorer_exi(explorer, exi_data, exi_size);- Compatible with exificient-generated EXI files
- Dynamic string table management
- Generic content classification
sxml_enable_entity_processing(explorer, 1); // Standard XML entities
sxml_enable_numeric_entities(explorer, 1); // A format
sxml_enable_extended_entities(explorer, 1); // © format
sxml_enable_namespace_processing(explorer, 1); // ns:tag format- Configuration file parsing on microcontrollers
- IoT sensor data processing
- Embedded web service responses
- Simple data exchange protocols
BSD 4-Clause License - see LICENSE file.