PHPackages                             donatorsky/php-xml-template-reader - 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. donatorsky/php-xml-template-reader

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

donatorsky/php-xml-template-reader
==================================

The PHP XML Reader where you show how to read the XML, and it does the rest for you.

v0.1.0(5y ago)25MITPHPPHP ^7.4|^8.0

Since May 9Pushed 5y ago1 watchersCompare

[ Source](https://github.com/donatorsky/php-xml-template-reader)[ Packagist](https://packagist.org/packages/donatorsky/php-xml-template-reader)[ Docs](https://github.com/donatorsky/php-xml-template-reader)[ Fund](https://ko-fi.com/donatorsky)[ RSS](/packages/donatorsky-php-xml-template-reader/feed)WikiDiscussions main Synced 1w ago

READMEChangelog (1)Dependencies (15)Versions (2)Used By (0)

PHP XML Template Reader
=======================

[](#php-xml-template-reader)

The PHP XML Reader where you show how to read the XML, and it does the rest for you.

[![GitHub license](https://camo.githubusercontent.com/074b89bca64d3edc93a1db6c7e3b1636b874540ba91d66367c0e5e354c56d0ea/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e737667)](https://github.com/donatorsky/php-xml-template-reader/blob/main/LICENSE)[![Build](https://github.com/donatorsky/php-xml-template-reader/workflows/CI/badge.svg?branch=main)](https://github.com/donatorsky/php-xml-template-reader/actions?query=branch%3Amain)[![Coverage Status](https://camo.githubusercontent.com/2dc18512dcc49e810c9ed49515f7fc29ade9b6ef918dcae5bf1fb21305320256/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f646f6e61746f72736b792f7068702d786d6c2d74656d706c6174652d7265616465722f62616467652e7376673f6272616e63683d6d61696e)](https://coveralls.io/github/donatorsky/php-xml-template-reader?branch=main)

How it works
------------

[](#how-it-works)

The PHP XML Template Reader helps You to parse given XML file and create an object from it. The parser uses given template as a schema and tries to match it to input XML, optionally validating it with defined rules.

To start, simply create new `\Donatorsky\XmlTemplate\Reader\XmlTemplateReader` object, pass it the template and read using one of available reading modes.

### Example 1

[](#example-1)

Assuming the following XML:

```

        Lorem ipsum adventures
        ...

    ...

```

You can already see a pattern, so You can define the template as follows:

```

            ...

        ...

```

As the output You will see an object of type `\Donatorsky\XmlTemplate\Reader\Models\Node` (by default, can be changed) with processed data:

```
Node {
    private $nodeName = 'books';

    private $children = Map [
        // Because of tpl:type="collection", book element is expected to occur more than 1 times
        'book' => Collection [
            0 => Node {
                    private $nodeName = 'book';

                    private $attributes = Map [
                        // You can define set of parsing rules in the template. In this case:
                        // required | integer
                        // Means, that value cannot be empty and has to be a valid number. It is also converted to the integer.
                        'ISBN' => 1234567890,

                        // No filters defined means the value is read "as is"
                        'category' => 'adventures',
                    ];

                    // By default, tpl:type="single", so title is expected to occur at most 1 time
                    private $relations = Map [
                        'title' => Node {
                                        private $nodeName = 'title';

                                        private $contents = 'Lorem ipsum adventures';
                                    },
                        ...
                    ];
                },
            1 => ...
        ]
    ];
}
```

Please note, that only nodes defined in the template are present in the output Node. When XML changes, You need to update the template.

### Example 2

[](#example-2)

The Reader supports namespaced nodes and attributes. In case suggested template's `tpl` namespace conflicts with Yours, feel free to change it to any other XML valid value:

```

        Lorem ipsum adventures
        ...

    ...

```

You can already see a pattern, so You can define the template as follows:

```

            ...

        ...

```

Reading modes
-------------

[](#reading-modes)

Multiple reading modes are available. Given the following example code:

```
$xmlTemplateReader = new \Donatorsky\XmlTemplate\Reader\XmlTemplateReader(
