Skip to main content
The XMLBuilder class provides the foundation for building XML documents. It’s extended by ArrayExporter and ViewExporter.

Constructor

new XMLBuilder(string $encoding = 'UTF-8', string $version = '1.0')
encoding
string
default:"UTF-8"
The encoding to use for the XML document.
version
string
default:"1.0"
The XML version to use.
$builder = new XMLBuilder('UTF-8', '1.0');
You typically don’t use XMLBuilder directly. Use XML::export() or XML::exportView() instead.

Configuration Methods

version()

Set the XML version.
public function version(string $version = "1.0"): XMLBuilder
version
string
default:"1.0"
The XML version string.
return
XMLBuilder
Returns the builder instance for chaining.
$builder->version('1.1');

encoding()

Set the XML encoding.
public function encoding(string $encoding = "UTF-8"): XMLBuilder
encoding
string
default:"UTF-8"
The character encoding (e.g., UTF-8, ISO-8859-1).
return
XMLBuilder
Returns the builder instance for chaining.
$builder->encoding('ISO-8859-1');

rootTag()

Set the name of the root XML tag.
public function rootTag(string $name = "root"): XMLBuilder
name
string
default:"root"
The name for the root tag.
return
XMLBuilder
Returns the builder instance for chaining.
$builder->rootTag('users');
// Produces: <users>...</users>

setRootTag()

Set or disable the root tag.
public function setRootTag(string|bool $tag): static
tag
string|bool
required
The root tag name, or false to disable the root tag.
return
static
Returns the builder instance for chaining.
$builder->setRootTag('data');
$builder->setRootTag(false); // Disable root tag

disableRootTag()

Disable the root tag wrapper.
public function disableRootTag(): static
return
static
Returns the builder instance for chaining.
$builder->disableRootTag();
// No root tag will be added
Disabling the root tag may produce invalid XML if you have multiple top-level elements.

itemName()

Set the default name for items without explicit keys.
public function itemName(string $name = "item"): XMLBuilder
name
string
default:"item"
The default name for array items without keys.
return
XMLBuilder
Returns the builder instance for chaining.
$builder->itemName('record');

Data Methods

data()

Set the data to be exported.
public function data(array $data): XMLBuilder
data
array
required
The array data to convert to XML.
return
XMLBuilder
Returns the builder instance for chaining.
$builder->data([
    'user' => [
        'name' => 'John',
        'email' => '[email protected]'
    ]
]);

forceItemName()

Force usage of the item name instead of auto-generating names based on the root tag.
public function forceItemName(): XMLBuilder
return
XMLBuilder
Returns the builder instance for chaining.
$builder->forceItemName()->itemName('entry');

Protected Methods

These methods are used internally by exporters but can be useful to understand the XML generation process.

getProlog()

Generate the XML prolog (declaration).
protected function getProlog(): string
return
string
Returns the XML prolog string.
// Produces: <?xml version="1.0" encoding="UTF-8"?>

openRootTag()

Generate the opening root tag.
protected function openRootTag(): string
return
string
Returns the opening tag or empty string if root tag is disabled.
// Produces: <root>

closeRootTag()

Generate the closing root tag.
protected function closeRootTag(): string
return
string
Returns the closing tag or empty string if root tag is disabled.
// Produces: </root>

getRootTag()

Get the current root tag name.
protected function getRootTag(): string
return
string
Returns the root tag name (defaults to ‘root’).

getFieldName()

Generate the name for top-level tags.
protected function getFieldName(string|int $field): string
field
string|int
required
The field name or numeric index.
return
string
Returns the generated field name.

generateFieldName()

Generate field names for numeric keys.
protected function generateFieldName(string $field): string
field
string
required
The parent field name.
return
string
Returns the singular form of the field name or the configured item name.

Magic Methods

__call()

Handle dynamic setters for configuration properties.
public function __call(string $name, array $arguments): XMLBuilder
name
string
required
The method name (must be one of: version, rootTag, encoding, itemName).
arguments
array
required
The method arguments (exactly one argument required).
return
XMLBuilder
Returns the builder instance for chaining.
$builder->version('1.0')
        ->encoding('UTF-8')
        ->rootTag('data')
        ->itemName('record');

Complete Example

use Flowgistics\XML\XMLBuilder;

$builder = new XMLBuilder();

$xml = $builder
    ->version('1.0')
    ->encoding('UTF-8')
    ->rootTag('users')
    ->itemName('user')
    ->data([
        ['name' => 'John', 'email' => '[email protected]'],
        ['name' => 'Jane', 'email' => '[email protected]']
    ]);
In practice, use XML::export() which returns an ArrayExporter that extends XMLBuilder with additional export functionality.

Inheritance

The following classes extend XMLBuilder:
  • ArrayExporter - Exports arrays to XML
  • ViewExporter - Exports Blade views to XML
See their respective documentation pages for export-specific methods.

Build docs developers (and LLMs) love