Download the PHP package donatorsky/php-xml-template-reader without Composer

On this page you can find all versions of the php package donatorsky/php-xml-template-reader. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package 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 Build Coverage Status

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

Assuming the following XML:

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:

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

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:

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

Reading modes

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

read: read XML from string

You can provide XML contents and parse it using read method:

readFile: read XML from file in given path

You can provide a path to the XML file and parse it using readFile method:

readStream: read XML from already opened resource

You can provide a resource with the XML contents and parse it using readStream method:

open, update and close: custom stream XML reading

You can read the XML chunk by chunk using You own implementation with open, update and close methods:

Parsing modifiers

You can use various parsing modifiers to define some behaviours. Examples below use tpl namespace.

tpl:castTo

Accepted values: class' FQN, must implement \Donatorsky\XmlTemplate\Reader\Models\Contracts\NodeInterface.

By default, when node is parsed, it creates new \Donatorsky\XmlTemplate\Reader\Models\Node instance with parsed data. However, You can use Your own class. This class must implement \Donatorsky\XmlTemplate\Reader\Models\Contracts\NodeInterface interface.

Example

Define node classes:

Use them in the template:

The output:

tpl:collectAttributes

Accepted values: all, validated (default).

By default, only validated nodes' attributes are collected. This means, that only attributes that are defined in the template are collected. However, You can change it if You also want to collect other attributes.

Given the input XML:

Example 1

With the following template:

You will get:

Example 2

With the following template:

You will get:

tpl:contents

Accepted values: none (default when tpl:type = collection), raw (default when tpl:type = single), trimmed.

By default, no node's contents is collected (none). This is especially useful for nodes containing other nodes, thus the contents is only a bunch of whitespaces (when XML if pretty-printed). You can change this behaviour and collect raw, unchanged contents (raw) of the node or additionally trim whitespaces (trimmed).

Example

Given the input XML:

With the following template:

You will get:

tpl:type

Accepted values: single (default), collection.

By default, each node defined in the template is considered to be a single (single). However, if You expect multiple elements of the same type, You can change it (collection).

Example

Given the input XML:

With the following template:

You will get:

Rules

Rules are simple validators and transformers that can be chained. You can use rules to define attributes constraints and transform them to expected value. Rules may be aliased. Rules can accept additional attributes. Names and aliases are case-insensitive. You can examine built-in rules in src/Rules directory, or You can create one by implementing \Donatorsky\XmlTemplate\Reader\Rules\Contracts\RuleInterface interface.

Built-in rules

Rule name Aliases Validation Transformation
callback Custom validation rule Custom transformation
float Numeric value Cast to float type
greaterThan:threshold Numeric value greater than threshold Value unchanged
int integer Numeric value Cast to int type
numeric Numeric value Cast to int or float type
required Value must not be empty Value unchanged
trim String value Value is trimmed

Custom rules

To define custom rule You need to first create a class that implements RuleInterface:

Then, You need to register rule class:

And use it in the template:


All versions of php-xml-template-reader with dependencies

PHP Build Version
Package Version
Requires php Version ^7.4|^8.0
ext-libxml Version *
ext-simplexml Version *
ext-xml Version *
beberlei/assert Version ^3.3
symfony/event-dispatcher Version ^5.2
thecodingmachine/safe Version ^1.3
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package donatorsky/php-xml-template-reader contains the following files

Loading the files please wait ....