PHPackages                             mblarsen/array-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. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. mblarsen/array-xml

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

mblarsen/array-xml
==================

A simple yet expressive array syntax for building XML files

0.1.0(6y ago)19MITPHPPHP &gt;=7.2CI failing

Since Nov 21Pushed 6y ago1 watchersCompare

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

READMEChangelogDependencies (1)Versions (2)Used By (0)

array-xml
=========

[](#array-xml)

[![Build status](https://camo.githubusercontent.com/8efc8e8718c3cdf48de0953d11827e5a882365faf2b9be93e8f3d013a450148c/687474703a2f2f696d672e736869656c64732e696f2f7472617669732f6d626c617273656e2f61727261792d786d6c2e737667)](http://travis-ci.org/mblarsen/array-xml)

> Because DOMDocument and SimpleXML sucks

- Easily build XML with associative arrays
- Succinct syntax for *naming child elements*, *adding attributes*, and more
- You can combine with `DOMDocument` if you really have to

Examples
--------

[](#examples)

- Easy to create attributes
- Children takes name from parent by default

```
ArrayToXML::toXML(
    'Order@version=2.0' => [
        'ID@type=SKU' => 1234,
        'Lines' => [
            ['item' => 'ABC', 'qty' => 3],
            ['item' => 'DEF', 'qty' => 1],
        ]
    ]
);
```

Yields:

```

  1234

      ABC
      3

      DEF
      1

```

- Using `|` you can specify a differnt name of the children

```
ArrayToXML::toXML(
    'Order' => [
        'ID' => 1234,
        'Lines|OrderLine' => [
            ['item' => 'ABC', 'qty' => 3],
            ['item' => 'DEF', 'qty' => 1],
        ]
    ]
);
```

Yields:

```

  1234

      ABC
      3

      DEF
      1

```

- Using `name_mappers` you can use, index and child values to construct the child element name and attributes

```
ArrayToXML::toXML(
    'Order' => [
        'ID' => 1234,
        'Lines' => [
            ['item' => 'ABC', 'qty' => 3],
            ['item' => 'DEF', 'qty' => 1],
        ]
    ],
    [
        'name_mappers' => [
            'Line' => function ($name, $index, $value) {
                return $name . '@number=' . ($index + 1);
            }
        ]
    ]
);
```

Yields:

```

  1234

      ABC
      3

      DEF
      1

```

- "Flatten" the parent element and put its children in its place using ` [
        'ID' => 1234,
        // Node names will be 'Line'
        '
