PHPackages                             pdeans/xml-builder - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. pdeans/xml-builder

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

pdeans/xml-builder
==================

Simple and lightweight library to make generating XML a breeze.

v1.0.3(3y ago)993.2k↓20.9%42MITPHPPHP &gt;=5.3.3

Since Jan 13Pushed 1y ago2 watchersCompare

[ Source](https://github.com/mivaprofsrvcs/xml-builder)[ Packagist](https://packagist.org/packages/pdeans/xml-builder)[ RSS](/packages/pdeans-xml-builder/feed)WikiDiscussions master Synced 1w ago

READMEChangelog (2)DependenciesVersions (5)Used By (2)

Easy XML Builder
----------------

[](#easy-xml-builder)

Simple and lightweight library to make generating XML a breeze.

### Installation

[](#installation)

Install via [Composer](https://getcomposer.org/).

```
$ composer require pdeans/xml-builder

```

### Usage

[](#usage)

The XML builder library extends PHP's [XMLWriter](http://us3.php.net/manual/en/book.xmlwriter.php) extension. All [XMLWriter](http://us3.php.net/manual/en/example.xmlwriter-oop.php) object oriented API properties and methods are available for each XML builder instance.

First, instantiate a new XML builder class object:

```
use pdeans\Builders\XmlBuilder;

$bulder = new XmlBuilder;
```

The `create` method is used to generate an xml tag. The `create` method takes the name of the root element as the first argument, and an associative array consisting of the data to build the root attribute elements and/or child elements as the second argument.

Here is a simple example:

```
$xml = $builder->create('Category_Add', [
    '@tags' => [
        'Code' => 'Tools',
        'Name' => $builder->cdata('Class Tools and Skill Kits'),
    ],
]);
```

This will produce the following xml:

```

    Tools
    Class Tools and Skill Kits]]>

```

#### Parent/Child Elements

[](#parentchild-elements)

Notice how the array key-values function under the `@tags` array from the above example. The keys represent the xml element names, and the values represent the xml element values. Child tags can also be nested following this pattern with the parent element represented by the array key, and the array value consisting of an array of the child elements as key-value pairs. This pattern can be repeated as needed to nest subsequent child elements.

#### Element Value Helpers

[](#element-value-helpers)

The `cdata` helper method can be used to wrap an element value in a `` tag, while the `decimal` helper method can be used to format a decimal number into a standard decimal format, rounding to 2 decimals by default and stripping out commas. The `decimal` helper method accepts an optional second parameter to set the precision.

```
// Output:
echo $builder->cdata('Class Tools and Skill Kits');

// Output: 49.00
echo $builder->decimal(49.0000000);

// Output: 49.001
echo $builder->decimal(49.0005, 3);
```

#### Reserved Keys

[](#reserved-keys)

The `@tags` key represents one of 3 reserved keys (each containing shortcut key counterparts) that the xml builder uses to parse and generate the xml. The reserved keys are as follows:

**@attributes Key**
*Shortcut: **@a***

The `@attributes` key is used to create xml element attributes. The `@a` key is also supported as a shortcut for the `@attributes` key.

Examples:

```
$xml = $builder->create('CategoryProduct_Assign', [
    '@attributes' => [
        'category_code' => 'Food',
        'product_code'  => 'ale-gallon',
    ],
]);

$xml = $builder->create('CategoryProduct_Assign', [
    '@a' => [
        'category_code' => 'Food',
        'product_code'  => 'ale-gallon',
    ],
]);
```

XML Produced:

```

```

**@tags Key**
*Shortcut: **@t***

The `@tags` key accepts an associative array of data to build the root element's children. The `@t` key is also supported as a shortcut for the `@tags` key.

Examples:

```
$xml = $builder->create('ProductAttribute_Add', [
    '@a' => [
        'product_code' => 'chest',
    ],
    '@tags' => [
        'Code'   => 'lock',
        'Type'   => 'select',
        'Prompt' => $builder->cdata('Lock'),
    ],
]);

$xml = $builder->create('ProductAttribute_Add', [
    '@a' => [
        'product_code' => 'chest',
    ],
    '@t' => [
        'Code'   => 'lock',
        'Type'   => 'select',
        'Prompt' => $builder->cdata('Lock'),
    ],
]);
```

XML Produced:

```

    lock
    select
    Lock]]>

```

**@value Key**
*Shortcut: **@v***

The `@value` key explicitly sets an xml element value. Generally, this is only required on xml elements that require both attributes and a value to be set. The `@v` key is also supported as a shortcut for the `@value` key.

Examples:

```
$xml = $builder->create('Module', [
    '@attributes' => [
        'code' => 'customfields',
        'feature' => 'fields_prod',
    ],
    '@tags' => [
        'ProductField_Value' => [
            '@attributes' => [
                'product' => 'chest',
                'field' => 'armor_type',
            ],
            '@value' => 'wood',
        ],
    ],
]);

$xml = $builder->create('Module', [
    '@a' => [
        'code' => 'customfields',
        'feature' => 'fields_prod',
    ],
    '@t' => [
        'ProductField_Value' => [
            '@a' => [
                'product' => 'chest',
                'field' => 'armor_type',
            ],
            '@v' => 'wood',
        ],
    ],
]);
```

XML Produced:

```

    wood

```

Note that the `@tags` key is used on the first level only of the associative array of tag data, as it represents the child tag data, while the other two reserved keys can be used on any sub-level throughout the associative array.

#### Repeated Tags

[](#repeated-tags)

Sometimes repeated tags are used in xml, which does not play nice with associative array key-value pairs. To circumvent this, the element name is still passed as the array key, however, the array value consists of a sequential array of arrays with the tag data.

```
$xml = $builder->create('Order_Add', [
    '@t' => [
        'Charges' => [
            'Charge' => [
                [
                    'Type' => 'SHIPPING',
                    'Description' => 'Shipping: UPS Ground',
                    'Amount' => 5.95
                ],
                [
                    'Type' => 'TAX',
                    'Description' => 'Sales Tax',
                    'Amount' => 2.15
                ],
            ],
        ],
    ],
]);
```

XML Produced:

```

            SHIPPING
            Shipping: UPS Ground
            5.95

            TAX
            Sales Tax
            2.15

```

#### Self-closing Tags

[](#self-closing-tags)

To generate a self-closing element without attributes, pass a value of *null* as the array value.

```
$xml = $builder->create('Order_Add', [
    '@t' => [
        'TriggerFulfillmentModules' => null,
    ],
]);
```

XML Produced:

```

```

###  Health Score

40

—

FairBetter than 86% of packages

Maintenance32

Infrequent updates — may be unmaintained

Popularity38

Limited adoption so far

Community17

Small or concentrated contributor base

Maturity61

Established project with proven stability

 Bus Factor1

Top contributor holds 90.9% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~655 days

Total

4

Last Release

1102d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/7277991?v=4)[Patrick Stearns](/maintainers/pdeans)[@pdeans](https://github.com/pdeans)

![](https://avatars.githubusercontent.com/u/135266743?v=4)[mivaprofsrvcs](/maintainers/mivaprofsrvcs)[@mivaprofsrvcs](https://github.com/mivaprofsrvcs)

---

Top Contributors

[![pdeans](https://avatars.githubusercontent.com/u/7277991?v=4)](https://github.com/pdeans "pdeans (10 commits)")[![phpguru](https://avatars.githubusercontent.com/u/798500?v=4)](https://github.com/phpguru "phpguru (1 commits)")

---

Tags

xmlbuilderwriterxml-builderxml generatorxml writer

### Embed Badge

![Health badge](/badges/pdeans-xml-builder/health.svg)

```
[![Health](https://phpackages.com/badges/pdeans-xml-builder/health.svg)](https://phpackages.com/packages/pdeans-xml-builder)
```

###  Alternatives

[masterminds/html5

An HTML5 parser and serializer.

1.8k260.4M292](/packages/masterminds-html5)[jms/serializer

Library for (de-)serializing data of any complexity; supports XML, and JSON.

2.3k139.8M905](/packages/jms-serializer)[jms/metadata

Class/method/property metadata management in PHP

1.8k157.6M95](/packages/jms-metadata)[jms/serializer-bundle

Allows you to easily serialize, and deserialize data of any complexity

1.9k91.4M662](/packages/jms-serializer-bundle)[veewee/xml

XML without worries

1836.6M38](/packages/veewee-xml)[sabre/xml

sabre/xml is an XML library that you may not hate.

52933.7M139](/packages/sabre-xml)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
