kalle/xml

Compact XML library for PHP with immutable and streaming writers, tree and streaming readers, reader queries, canonicalization, DOM interop, import, and XSD validation

Maintainers

Package info

github.com/kalicki2k/xml

pkg:composer/kalle/xml

Statistics

Installs: 10

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

0.1.0 2026-04-19 02:00 UTC

This package is auto-updated.

Last update: 2026-04-19 11:11:49 UTC


README

kalle/xml is a compact, strict XML library for PHP 8.2+.

XmlBuilder builds immutable XML models. XmlWriter serializes complete XmlDocument values. StreamingXmlWriter is the separate imperative writer for incremental output to file and stream targets.

It provides:

  • XmlBuilder for immutable, tree-based XML construction
  • XmlWriter for serializing built documents to strings, files, or streams
  • StreamingXmlWriter for incremental XML output to files and streams
  • StreamingXmlReader for incremental, cursor-based XML reading, subtree extraction, and non-overlapping record iteration via readElements()
  • XmlReader for read-only traversal of existing XML
  • XmlCanonicalizer for deterministic canonical XML output across writer, reader, import, and DOM flows
  • XmlDomBridge plus DOM entry points on XmlReader for explicit DOM interop
  • findAll() and findFirst() for small namespace-aware element queries on the reader model
  • XmlImporter for importing reader results back into the writer model
  • XmlValidator for validating XML against XSD schemas

The package stays intentionally narrow in scope. It covers XML writing, streaming XML reading, read-only tree loading, small reader-side queries, deterministic canonicalization, explicit DOM interop, reader-to-writer import, and XSD validation without trying to replace DOM, wrap all of XPath, or become a broad XML framework.

Installation

composer require kalle/xml

Runtime requirements: ext-dom, ext-libxml, and ext-xmlreader.

Current Scope

Included:

  • tree-based XML building with XmlBuilder
  • document serialization with XmlWriter::toString(), toFile(), and toStream()
  • streaming XML writing with StreamingXmlWriter
  • streaming XML reading and non-overlapping record iteration with StreamingXmlReader
  • read-only XML loading with XmlReader
  • canonical XML output with XmlCanonicalizer
  • explicit DOM interop with XmlDomBridge and XmlReader::fromDomDocument() / fromDomElement()
  • small namespace-aware element queries with findAll() and findFirst()
  • reader-to-writer import with XmlImporter
  • compact XSD validation with XmlValidator

Out of scope:

  • mutation APIs for loaded XML
  • broad DOM or XPath wrapper APIs
  • XML-to-array or XML-to-object mapping
  • XML diff, patch, merge, or signature tooling
  • broad schema-framework features beyond compact XSD validation

Quick Example

<?php

declare(strict_types=1);

use Kalle\Xml\Builder\XmlBuilder;
use Kalle\Xml\Writer\XmlWriter;

$document = XmlBuilder::document(
    XmlBuilder::element('catalog')
        ->child(
            XmlBuilder::element('book')
                ->attribute('isbn', '9780132350884')
                ->child(XmlBuilder::element('title')->text('Clean Code')),
        ),
);

echo XmlWriter::toString($document);

When To Use Each API

  • Use XmlBuilder when you want to build an immutable XML tree in memory, reuse subtrees, or keep fixtures readable.
  • Use XmlWriter when you already have a built XmlDocument and want a string, file, or stream.
  • Use StreamingXmlWriter when output is incremental, large, or should go straight to a file or stream.
  • Use StreamingXmlReader when input is large or incremental and you only need cursor-style inspection, non-overlapping record-by-record processing through readElements(), subtree extraction, or filtered export.
  • Use XmlReader when you want a loaded tree for traversal, parent/child navigation, or queries.
  • Use XmlCanonicalizer when stable canonical XML output matters for snapshots, comparison, hashing, or deduplication.
  • Use DOM interop when writer-side or reader-side flows need to connect to existing DOMDocument or DOMElement values without adopting a mutable DOM wrapper.
  • Use reader queries when findAll() or findFirst() is clearer than repeated traversal.
  • Use XmlImporter when reader results need to move back into the writer-side model.
  • Use XmlValidator when XML must match an XSD schema.

That split is intentional: building, whole-document serialization, and incremental streaming stay separate so the package stays compact.

Documentation