d-shorkin/arrayable-xml

PHP library for parsing XML to array with laravel support

1.0.3 2019-09-17 19:01 UTC

This package is auto-updated.

Last update: 2024-10-18 06:29:06 UTC


README

PHP library for parsing XML to array. This library can formatting attributes of xml

Installation

Install via composer:

composer require d-shorkin/arrayable-xml

Usage

$xmlString = <<<XML
<root rootAttr="bar">
    <example testAttr="foo">1</example>
    <example>2</example>
</root>
XML;

$factory = new Dshorkin\ArrayableXml\ArrayableXmlFactory();
$arrayableXml = $factory->create($xmlString);

var_dump($arrayableXml->toArray());

Result:

array(2) {
  ["rootAttr"]=>
  string(3) "bar"
  ["children"]=>
  array(1) {
    ["example"]=>
    array(2) {
      [0]=>
      array(2) {
        ["testAttr"]=>
        string(3) "foo"
        ["text"]=>
        string(1) "1"
      }
      [1]=>
      array(1) {
        ["text"]=>
        string(1) "2"
      }
    }
  }
}

Laravel

If you does not use php artisan package:discover command. You need add ArrayableXmlProvider to config.

config/app.php

'providers' => [
    // ...
    
    Dshorkin\ArrayableXml\Laravel\ArrayableXmlProvider::class,
],

'aliases' => [
    // ...
    
    'ArrayableXml' => Dshorkin\\ArrayableXml\Laravel\ArrayableXml::class
]

Basic laravel usage:

ArrayableXml::create($xmlString)->toArray()

Class reference

Dshorkin\ArrayableXml\ArrayableXmlFactory

Create

ArrayableXmlFactory::create(string $str, [string $childrenFieldKey, [string $textFieldKey, [bool $miniElements]]]): ArrayableXmlInterface

Create From SimpleXmlElement

ArrayableXmlFactory::createFromSimpleXmlElement(\SimpleXMLElement $xml, [string $childrenFieldKey, [string $textFieldKey, [bool $miniElements]]]): ArrayableXmlInterface