2017 © Pedro Peláez
 

library xml-utils

XML utilities, mainly conversion between XML <=> array.

image

graze/xml-utils

XML utilities, mainly conversion between XML <=> array.

  • Tuesday, March 20, 2018
  • by graze
  • Repository
  • 11 Watchers
  • 0 Stars
  • 1,198 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 4 Versions
  • 65 % Grown

The README.md

xml-utils

Latest Version on Packagist Software License Build Status Coverage Status Quality Score Total Downloads, (*1)

XML utilities, mainly conversion between XML = array., (*2)

Install

Via Composer, (*3)

``` bash $ composer require graze/xml-utils, (*4)


## Usage ### Converting an array to XML ```php $array = [ 'child1' => 111, 'child2' => 222 ]; $xmlElement = new SimpleXMLElement('<root/>'); $xmlConverter = new Graze\XmlUtils\XmlConverter(); $xmlConverter->addArrayAsChildren($array, $xmlElement); echo $xmlElement->asXml();

result:, (*5)

?xml version="1.0"?>
<root><child1>111</child1><child2>222</child2></root>

Attributes

Attributes are supported by using @attributes key with an array of attribute name => value., (*6)

$array = [
    'child1' => [
        '@attributes' => [
            'id' => 123
        ]
    ]
];

result:, (*7)

<child1 id="123"/>

Attributes with value

If a value needs to be set for an element with an attribute then the @value key should be used., (*8)

$array = [
    'child1' => [
        '@attributes' => [
            'id' => 123
        ],
        '@value' => 'some description'
    ]
];

result:, (*9)

<child1 id="123">some description</child1>

Repeated elements

Repeated elements are supported with indexed arrays., (*10)

$array = [
    'child' => [
        'first',
        'second'
    ]
];

result:, (*11)

<child>first</child><child>second</child>

Converting XML to an array

$xml = '<root><child1>123</child1><child2 id="1"><subchild1>456</subchild1></child2></root>';

$xmlElement = new SimpleXMLElement($xml);

$xmlConverter = new XmlConverter();
$array = $xmlConverter->convertToArray($xmlElement);

result:, (*12)

[
    'child1' => '123',
    'child2' => [
        '@attributes' => [
            'id' => '1'
        ],
        'subchild1' => '456'
    ]
];

Formatting XML

Formats XML so it is easier to read., (*13)

$unformattedXml = '<root><child1>123</child1><child2 attribute="1"><subchild1>456</subchild1></child2></root>';

$xmlFormatter = new XmlFormatter();
$formattedXml = $xmlFormatter->format($unformattedXml);

result:, (*14)

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <child1>123</child1>
  <child2 attribute="1">
    <subchild1>456</subchild1>
  </child2>
</root>

Change log

Please see CHANGELOG for more information what has changed recently., (*15)

Testing

make build test

Contributing

Please see CONTRIBUTING for details., (*16)

Security

If you discover any security related issues, please email security@graze.com instead of using the issue tracker., (*17)

Credits

License

The MIT License (MIT). Please see License File for more information., (*18)

The Versions

20/03 2018

dev-master

9999999-dev https://github.com/graze/xml-utils

XML utilities, mainly conversion between XML <=> array.

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

by Brendan Kay

graze xml-utils

20/03 2018

v1.1.0

1.1.0.0 https://github.com/graze/xml-utils

XML utilities, mainly conversion between XML <=> array.

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

by Brendan Kay

graze xml-utils

20/03 2018

v1.0.1

1.0.1.0 https://github.com/graze/xml-utils

XML utilities, mainly conversion between XML <=> array.

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

by Brendan Kay

graze xml-utils

07/03 2018

v1.0.0

1.0.0.0 https://github.com/graze/xml-utils

XML utilities, mainly conversion between XML <=> array.

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

by Brendan Kay

graze xml-utils