sabre/xml
A specialized, mapping-based XML reader and writer for PHP built on XMLReader and XMLWriter.
Repository Health
Technical Analysis
sabre/xml is an XML library that maps XML elements to PHP objects and values and back, wrapping PHP’s low-level XMLReader and XMLWriter streams behind an ergonomic, declarative API. Instead of loading an entire document into a DOM tree, it reads and writes XML as a stream, keeping memory use low even for large documents.
Originally built to power the SabreDAV CalDAV/CardDAV servers, the library lets you register element deserializers and serializers so complex namespaced XML maps cleanly onto native PHP data structures.
What You Get
- A streaming Reader that maps XML elements to PHP arrays and objects
- A Writer that serializes PHP structures back into namespaced XML
- A central Service for registering element mappings and namespaces
- XmlSerializable and XmlDeserializable interfaces for custom element handling
- Typed exceptions (ParseException, LibXMLException) for robust error handling
Common Use Cases
- Parsing large or namespaced XML documents with low memory overhead
- Building CalDAV, CardDAV, or WebDAV request and response payloads
- Mapping XML API responses onto native PHP data structures
- Generating well-formed, namespace-aware XML from application data
Under The Hood
Architecture - The library centers on lib/Service.php, which holds a registry of element mappings and namespace value objects and hands them to a Reader (extending PHP’s XMLReader) and a Writer (extending XMLWriter). Deserialization and serialization logic live in the Deserializer/ and Serializer/ namespaces, invoked through the XmlDeserializable/XmlSerializable interfaces so custom elements control their own mapping. A ContextStackTrait tracks namespace and element-mapping context as the stream is walked.
Tech Stack - Pure PHP requiring PHP ^8.2 plus the ext-xmlreader, ext-xmlwriter, and ext-dom extensions and libxml >= 2.6.20, with a single runtime dependency on sabre/uri. Tooling includes PHPUnit, PHPStan with strict rules, Rector, and php-cs-fixer.
Code Quality - The code is strongly typed (major version 3 introduced full type declarations), enforced by a PHPStan strict-rules configuration with a baseline, and covered by a tests/ suite run under PHPUnit in CI. Naming follows clear Reader/Writer/Service roles and the small surface area keeps each class focused.
API Design - The public API is deliberately small: instantiate a Service, register element maps, then call parse/write. The mapping-by-callback model is powerful but has a learning curve for complex namespaced schemas, which the official sabre.io documentation covers with reading/writing guides. Once understood, it removes almost all boilerplate compared to raw XMLReader usage.