2017 © Pedro Peláez
 

library marshal-xml-serializer

Marshal XML Serializer will directly serialize the data to XML, it is built on top of the Marshal Serializer.

image

kingson-de/marshal-xml-serializer

Marshal XML Serializer will directly serialize the data to XML, it is built on top of the Marshal Serializer.

  • Tuesday, March 20, 2018
  • by Kingson-de
  • Repository
  • 1 Watchers
  • 1 Stars
  • 6 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 5 Versions
  • 0 % Grown

The README.md

Marshal XML Serializer

Marshal Serializer logo, (*1)

License Build Status Code Coverage Scrutinizer Code Quality, (*2)

Introduction

Marshal XML is serializing / marshalling data structures to XML. It is also deserializing / unmarshalling XML back to the data structures., (*3)

Installation

Easiest way to install the library is via composer:, (*4)

composer require kingson-de/marshal-xml-serializer

The following PHP versions are supported: * PHP 7.0 * PHP 7.1 * PHP 7.2 * PHP 7.3, (*5)

Execute tests

Just run:, (*6)

composer test

Or without code coverage:, (*7)

composer quicktest

Usage

How to create Data Structures which can be serialized?

Please check the Marshal Serializer README for more information., (*8)

How to use the Marshal XML Serializer library?

The library provides several static methods to create your XML data once you defined the data structures., (*9)

 [
            'username'  => $user->getUsername(),
            'email'     => $user->getEmail(),
            'birthday'  => $user->getBirthday()->format('Y-m-d'),
            'followers' => count($user->getFollowers()),
        ],
    ];
}, $user);
```

Be aware `MarshalXml::serializeCollection` and `MarshalXml::serializeCollectionCallable` methods are not available.
Collections in XML cannot be generated at root level.
But after defining the root node you can use collections anywhere.

#### How to define XML attributes?

If you are using a concrete implementation that is extending AbstractXmlMapper you can use the `attributes` method.

```php
 [
                $this->attributes() => [
                    'xmlns' => 'http://example.org/xml',
                ],
            ],
        ];
    }
}
```

If you are using a callable you need to use the `MarshalXml::ATTRIBUTES_KEY` constant.

```php
 [
            MarshalXml::ATTRIBUTES_KEY => [
                'xmlns' => 'http://example.org/xml',
            ],
        ],
    ];
});
```

This will generate:

```xml

<root xmlns="http://example.org/xml"/>

How to define XML node values?

This is pretty simple:, (*10)

 [
            'user' => $user->getUsername(),
        ],
    ];
}, $user);
```

This will generate:

```xml

<root>
  <user>Kingson</user>
</root>

How to define XML node values as CDATA?

Then you must use the cdata method for concrete Mapper implementations or MarshalXml::CDATA_KEY for callable., (*11)

 [
                'user' => $this->cdata($user->getUsername()),
            ],
        ];
    }
}
```

```php
 [
            'user' => [
                MarshalXml::CDATA_KEY => $user->getUsername(),
            ],
        ],
    ];
}, $user);
```

This will generate:

```xml

<root>
  <user></user>
</root>

But how to define XML node values if the node also has attributes?

Then you must use the data \/ cdata method for concrete Mapper implementations or MarshalXml::DATA_KEY \/ MarshalXml::CDATA_KEY for callable., (*12)

 [
                'user' => [
                    $this->attributes() => [
                        'xmlns' => 'http://example.org/xml',
                    ],
                    $this->data() => $user->getUsername(),
                ],
                'userCDATA' => [
                    $this->attributes() => [
                        'xmlns' => 'http://example.org/xml',
                    ],
                    $this->cdata() => $user->getUsername(),
                ],
            ],
        ];
    }
}
```

```php
 [
            'user' => [
                MarshalXml::ATTRIBUTES_KEY => [
                    'xmlns' => 'http://example.org/xml',
                ],
                MarshalXml::DATA_KEY => $user->getUsername(),
            ],
            'userCDATA' => [
                MarshalXml::ATTRIBUTES_KEY => [
                    'xmlns' => 'http://example.org/xml',
                ],
                MarshalXml::CDATA_KEY => $user->getUsername(),
            ],
        ],
    ];
}, $user);
```

This will generate:

```xml

<root>
  <user xmlns="http://example.org/xml">Kingson</user>
  <userCDATA xmlns="http://example.org/xml"></userCDATA>
</root>

Deserializing / Unmarshalling

To transform XML back to your structure use Marshal's deserialize functions. You need a class extending the AbstractObjectMapper which will be passed to the deserializeXml function., (*13)

get('container')
            ->get('acme-example:config')
            ->get('acme-example:id')
            ->get(MarshalXml::CDATA_KEY);
    }
}
```

```php
<root><tag>Hello World!</tag></root>';

$flexibleData = new FlexibleData(MarshalXml::deserializeXmlToData($xml));

$flexibleData['root']['tag'] = 'some other text';

$modifiedXml = MarshalXml::serialize($flexibleData);

License

This project is released under the terms of the Apache 2.0 license., (*14)

The Versions

20/03 2018

dev-deserializer

dev-deserializer

Marshal XML Serializer will directly serialize the data to XML, it is built on top of the Marshal Serializer.

  Sources   Download

Apache-2.0

The Requires

 

The Development Requires

by Martin Ossowski

xml serialization serializer marshalling marshal mappers cdata

09/03 2018

dev-master

9999999-dev

Marshal XML Serializer will directly serialize the data to XML, it is built on top of the Marshal Serializer.

  Sources   Download

Apache-2.0 Apache License

The Requires

 

The Development Requires

by Martin Ossowski

xml serialization serializer marshalling marshal mappers cdata

09/03 2018

v1.1.0

1.1.0.0

Marshal XML Serializer will directly serialize the data to XML, it is built on top of the Marshal Serializer.

  Sources   Download

Apache License

The Requires

 

The Development Requires

by Martin Ossowski

xml serialization serializer marshalling marshal mappers cdata

22/12 2017

v1.0.1

1.0.1.0

Marshal XML Serializer will directly serialize the data to XML, it is built on top of the Marshal Serializer.

  Sources   Download

Apache License

The Requires

 

The Development Requires

by Martin Ossowski

xml serialization serializer marshalling marshal mappers

21/12 2017

v1.0.0

1.0.0.0

Translates data structures to XML.

  Sources   Download

Apache License

The Requires

 

The Development Requires

by Martin Ossowski