Marshal XML Serializer
, (*1)
, (*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)