PHPackages                             darkfriend/php5-xml - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. darkfriend/php5-xml

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

darkfriend/php5-xml
===================

PHP5 library for encode/decode xml

2.0.0(5y ago)02.3k2PHPPHP &gt;=5.3CI failing

Since Jan 12Pushed 5y ago1 watchersCompare

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

READMEChangelog (4)Dependencies (3)Versions (5)Used By (2)

PHP5 library for encode/decode xml
==================================

[](#php5-library-for-encodedecode-xml)

`composer require darkfriend/php5-xml`

- Array to XML (`XML::encode()`)
- XML to Array (`XML::decode()`)

### Other php version

[](#other-php-version)

- [Xml encode/decode for php7](https://github.com/darkfriend/php7-xml)

How to install
--------------

[](#how-to-install)

```
composer require darkfriend/php5-xml

```

Array Structure
---------------

[](#array-structure)

- elementName - basic example
    - @attributes
        - `key => value` (array)
    - cdata - multiline text
    - value - text or tree elements
- elementName - example element with attachments
    - @attributes
        - `key => value` (array)
    - value
        - elementName
            - @attributes
                - `key => value` (array)
            - value - text or tree elements
        - elementName
            - @attributes
                - `key => value` (array)
            - value - text or tree elements
- elementName - example element with duplicate attachments
    - @attributes
        - `key => value` (array)
    - value
        - elementName - duplicate element (array)
            - 0
                - @attributes
                    - `key => value` (array)
                - value - text or tree elements
            - 1
                - @attributes
                    - `key => value` (array)
                - value - text or tree elements
- elementName - example element duplicate
    - 0
        - @attributes
            - `key => value` (array)
        - value - text or tree elements
    - 1
        - @attributes
            - `key => value` (array)
        - value - text or tree elements

### Encode Events

[](#encode-events)

- afterChild - execute function when added child element

How to use
----------

[](#how-to-use)

### Array to XML (encode)

[](#array-to-xml-encode)

```
$array = array(
    'bar' => 'value bar',
    'foo' => 'value foo',
    'der' => array(
        '@attributes' => array(
            'at1' => 'at1val',
            'at2' => 'at2val',
        ),
        'cdata' => 'this is long text',
    ),
    'qpo' => array(
        '@attributes' => array(
            'test1' => 'valTest',
        ),
        'value' => array(
            'sub1' => array(
                'value' => array(
                    'sub2'=>'val'
                )
            ),
            'multi-sub2' => array(
                array(
                    '@attributes' => array(
                        'at1' => 'val'
                    ),
                    'value' => array(
                        'multi-sub2-sub3' => array(
                            'value' => 'value multi-sub2'
                        )
                    )
                ),
                array(
                    'value' => 'value multi-sub2'
                ),
            ),
        ),
    ),
    'duplicateElement' => array(
        array(
            '@attributes' => array(
                'atDuplicate1' => 'val'
            ),
            'value' => 'valueDuplicateElement1',
        ),
        array(
            '@attributes' => array(
                'atDuplicate2' => 'val'
            ),
            'value' => 'valueDuplicateElement2',
        ),
        array(
            '@attributes' => array(
                'atDuplicate3' => 'val'
            ),
            'value' => array(
                'subElement' => 'val'
            ),
        ),
    )
);

echo \darkfriend\helpers\Xml::encode($array);

// example with use events
\darkfriend\helpers\Xml::encode($array, [
    'afterChild' => function($xml, $child, $name, $params) {
        // your code
        return $child;
    },
]);
```

#### Result encode

[](#result-encode)

```

    value bar
    value foo

         this is long text ]]>

            val

            value multi-sub2

        value multi-sub2

    valueDuplicateElement1
    valueDuplicateElement2

        val

```

### Xml string to Array (decode)

[](#xml-string-to-array-decode)

```
$xml = '

    value bar
    value foo

            val

            value multi-sub2

        value multi-sub2

    valueDuplicateElement1
    valueDuplicateElement2

        val

';

var_dump(\darkfriend\helpers\Xml::decode($xml));
```

#### Result decode

[](#result-decode)

```
array(5) {
  ["bar"]=>
  array(1) {
    [0]=>
    array(1) {
      ["value"]=>
      string(9) "value bar"
    }
  }
  ["foo"]=>
  array(1) {
    [0]=>
    array(1) {
      ["value"]=>
      string(9) "value foo"
    }
  }
  ["der"]=>
  array(1) {
    [0]=>
    array(2) {
      ["@attributes"]=>
      array(2) {
        ["at1"]=>
        string(6) "at1val"
        ["at2"]=>
        string(6) "at2val"
      }
      ["value"]=>
      string(17) "this is long text"
    }
  }
  ["qpo"]=>
  array(1) {
    [0]=>
    array(2) {
      ["@attributes"]=>
      array(1) {
        ["test1"]=>
        string(7) "valTest"
      }
      ["value"]=>
      array(2) {
        ["sub1"]=>
        array(1) {
          [0]=>
          array(1) {
            ["value"]=>
            string(0) ""
          }
        }
        ["multi-sub2"]=>
        array(2) {
          [0]=>
          array(1) {
            ["@attributes"]=>
            array(1) {
              ["at1"]=>
              string(3) "val"
            }
          }
          [1]=>
          array(1) {
            ["value"]=>
            string(16) "value multi-sub2"
          }
        }
      }
    }
  }
  ["duplicateElement"]=>
  array(3) {
    [0]=>
    array(2) {
      ["@attributes"]=>
      array(1) {
        ["atDuplicate1"]=>
        string(3) "val"
      }
      ["value"]=>
      string(22) "valueDuplicateElement1"
    }
    [1]=>
    array(2) {
      ["@attributes"]=>
      array(1) {
        ["atDuplicate2"]=>
        string(3) "val"
      }
      ["value"]=>
      string(22) "valueDuplicateElement2"
    }
    [2]=>
    array(2) {
      ["@attributes"]=>
      array(1) {
        ["atDuplicate3"]=>
        string(3) "val"
      }
      ["value"]=>
      array(1) {
        ["subElement"]=>
        array(1) {
          [0]=>
          array(1) {
            ["value"]=>
            string(3) "val"
          }
        }
      }
    }
  }
}

```

### Custom &lt;?xml ?&gt; attributes and custom &lt;root&gt;

[](#custom-xml--attributes-and-custom-root)

```
$array = array(
    'bar' => 'value bar',
    'foo' => 'value foo',
    'der' => array(
        '@attributes' => array(
            'at1' => 'at1val',
            'at2' => 'at2val',
        ),
        'value' => 'this is long text',
    ),
);

echo \darkfriend\helpers\Xml::encode(
    $array,
    array(
        'root' => '', // custom root element with custom attribute atExample
        'prolog' => array(
            'attributes' => array(
                'version' => '1.0',
                'encoding' => 'utf-8',
            ),
            'elements' => array( // additional elements for prolog
                /*''*/
                ' '
            ),
        ),
    )
);
```

```

    value bar
    value foo
    this is long text

```

### Custom root element

[](#custom-root-element)

```
$array = array(
    'bar' => 'value bar',
    'foo' => 'value foo',
    'der' => array(
        'cdata' => 'this is long text',
        '@attributes' => array(
            'at1' => 'at1val',
            'at2' => 'at2val',
        ),
    )
);

echo \darkfriend\helpers\Xml::encode(
    $array,
    array(
        'root' => '',
    )
);
```

```

    value bar
    value foo
    this is long text]]>

```

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity16

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity55

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% 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 ~134 days

Total

4

Last Release

1916d ago

Major Versions

1.0.2 → 2.0.02021-02-17

### Community

Maintainers

![](https://www.gravatar.com/avatar/302f1cdc7cb85fc89550bad0c164e195b831f50de8a2a148fa754670d5f328cc?d=identicon)[darkfriend](/maintainers/darkfriend)

---

Top Contributors

[![darkfriend](https://avatars.githubusercontent.com/u/7825114?v=4)](https://github.com/darkfriend "darkfriend (6 commits)")

---

Tags

arrayhelperphpphp5xmlxml-decoderxml-encoderphphelperdevxmldevelopmentphp5array-to-xmlxml-to-arrayxml\_encodexml\_decodedarkfriend

###  Code Quality

TestsCodeception

### Embed Badge

![Health badge](/badges/darkfriend-php5-xml/health.svg)

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

###  Alternatives

[barryvdh/laravel-ide-helper

Laravel IDE Helper, generates correct PHPDocs for all Facade classes, to improve auto-completion.

14.9k123.0M687](/packages/barryvdh-laravel-ide-helper)[veewee/xml

XML without worries

1835.9M29](/packages/veewee-xml)[zjkal/time-helper

一个简单快捷的PHP日期时间助手类库。 a smart PHP datetime helper library.

21128.6k1](/packages/zjkal-time-helper)

PHPackages © 2026

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