XAO SPECIFICATION HANDBOOK

Xao Framework Specification · Release 1.0.0-dev

Database Aggregation (DbToXml)

Convert PDO queries, PDOStatement results, and PHP iterables directly into structured DomDoc XML fragments under PHC.

Document Slug
database-aggregation
Section
Framework Reference
Audience
Backend developers integrating SQLite or SQL databases
Status
stable
Source File
content/database-aggregation.md
XML Contract
/xml/database-aggregation

Xao\DbToXml replaces Xao's legacy 2003 PEAR DB ResultFetcher with modern PDO / PDOStatement and PHP iterable serialization, producing clean DomDoc XML trees ready to graft into an application document.

API Methods

Source: src/DbToXml.php (extends XaoRoot)

Example: Grafting SQLite Rows Into a Page Document

<?php

declare(strict_types=1);

use Xao\DbToXml;
use Xao\DomDoc;

$pdo = new PDO('sqlite:' . __DIR__ . '/data/catalog.sqlite');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$db = new DbToXml();
$itemsDoc = $db->fromQuery(
    $pdo,
    'SELECT slug, title, category, summary FROM items WHERE category = :cat ORDER BY title',
    [':cat' => 'core'],
    'items',
    'item'
);

$page = new DomDoc('catalog');
$page->ndConsumeDoc($itemsDoc);
echo $page->toXml(true);

Error Handling & Parity

Invalid SQL queries, connection errors, or invalid XML tag names throw Xao\Exceptions\DbToXmlException (or record structured error state on XaoRoot) rather than crashing silently or exposing raw database exceptions to HTTP clients.

PHC Sandbox Requirements

When running file-backed SQLite databases under phc serve, configure phc.toml:

[sandbox]
allowed_roots = ["public", "src", "stylesheets", "data"]
allow_file_databases = true

Both allow_file_databases = true and listing the database directory in allowed_roots are required by PHC's deny-by-default sandbox.

Complete Handbook Index