2017 © Pedro Peláez
 

library xml-io

Library for parsing and generating XML

image

sergeynezbritskiy/xml-io

Library for parsing and generating XML

  • Monday, March 12, 2018
  • by sergeynezbritskiy
  • Repository
  • 1 Watchers
  • 0 Stars
  • 220 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 1 Open issues
  • 8 Versions
  • 22 % Grown

The README.md

xml-io

Library for Parsing xml into php array using easy mapping. It allows you to parse simple data like strings and numbers, arrays or a list of items and complex data like objects. Also any combination of these data types is allowed., (*1)

Build Status Latest Stable Version Total Downloads Latest Unstable Version License, (*2)

Installation

The easiest way to install module is using Composer, (*3)

composer require sergeynezbritskiy/xml-io:^5.0.0

Simple usage

The most useful test cases can be seen in tests, (*4)

Here is the most generic example. Lets pretend we have such xml as below, (*5)


<user id="1">
    <name>Sergey</name>
    <born format="ISO">1988-20-12</born>
    <passport id="MN123456">
        <date>2000-12-12</date>
    </passport>
    <keywords>
        <keyword>buono</keyword>
        <keyword>brutto</keyword>
        <keyword>cattivo</keyword>
    </keywords>
    <addresses>
        <address>
            <city>Kharkiv</city>
            <country>Ukraine</country>
        </address>
        <address>
            <city>London</city>
            <country>Great Britain</country>
        </address>
    </addresses>
</user>

Here is an example of how to convert such xml into array:, (*6)

$xmlString = ' xml string from above ';
$xmlReader = new \SergeyNezbritskiy\XmlIo\XmlReader();
$user = $xmlReader->stringToArray($xmlString, [
    //array element with key `id` will be created from attribute `id`
    'id' => '@id',
    //array element with key `name` will be created from tag `name`
    'name' => 'name',
    'born' => 'born',
    'born_format' => 'born.@format',
    'passport' => [
        'id' => '@id',
        'date' => 'date',
    ],
    //create simple list of items
    'keywords as keywords.keyword' => '{list}',
    //create element `addresses` which will be an array of associative arrays
    'addresses as addresses.address[]' => [
        'city' => 'city',
        'country' => 'country',
    ]
]);

the result will be smth like that:
$user = [
    'id' => '1',
    'name' => 'Sergey',
    'born' => '1988-20-12',
    'born_format' => 'ISO',
    'passport' => [
        'id' => 'MN123456',
        'date' => '2000-12-12',
    ],
    'keywords' => [
        'buono', 
        'brutto', 
        'cattivo'
    ],
    'addresses' => [
        [
            'city' => 'Kharkiv', 
            'country' => 'Ukraine'
        ],[
            'city' => 'London', 
            'country' => 'Great Britain'
        ],
    ]
];

and back, convert array to xml
$xmlWriter = new \SergeyNezbritskiy\XmlIo\XmlWriter();
$xml = $xmlWriter->toXmlString($user, 
'user' => [
    'attributes' => ['id'],
    'children' => [
        'name',
        'born',
        'passport' => [
            'attributes' => ['id'],
            'children' => 'date',
        ],
        'keywords' => [
            'children' => [
                'keyword[]' => [
                    'dataProvider' => 'keywords',
                    'text' => '{self}',
                ],
            ],
        ],
        'addresses' => [
            'children' => [
                'address[]' => [
                    'dataProvider' => 'addresses',
                    'children' => ['city', 'country'],
                ],
            ],
        ],
    ],
]);

Inspiration

Author was inspired for creating of this library by https://github.com/laravie/parser, (*7)

The Versions

12/03 2018

dev-master

9999999-dev

Library for parsing and generating XML

  Sources   Download

MIT

The Requires

  • php ^7.0
  • ext-simplexml *

 

The Development Requires

12/03 2018

v5.0.0

5.0.0.0

Library for parsing and generating XML

  Sources   Download

MIT

The Requires

  • php ^7.0
  • ext-simplexml *

 

The Development Requires

10/03 2018

v4.0.0

4.0.0.0

Library for parsing and generating XML

  Sources   Download

MIT

The Requires

  • php ^7.0
  • ext-simplexml *

 

The Development Requires

08/03 2018

v3.0.1

3.0.1.0

Library for parsing and generating XML

  Sources   Download

MIT

The Requires

  • php ^7.0
  • ext-simplexml *

 

The Development Requires

08/03 2018

v3.0.0

3.0.0.0

Library for parsing and generating XML

  Sources   Download

MIT

The Requires

  • php ^7.0
  • ext-simplexml *

 

The Development Requires

06/03 2018

v2.0.1

2.0.1.0

Library for parsing and generating XML

  Sources   Download

MIT

The Development Requires

06/03 2018

v2.0.0

2.0.0.0

Library for parsing and generating XML

  Sources   Download

MIT

The Development Requires

05/03 2018

v1.0.0

1.0.0.0

Library for parsing and generating XML

  Sources   Download

MIT

The Development Requires