XML utilities, mainly conversion between XML <=> array.
Via Composer
$ composer require graze/xml-utils
$array = [
'child1' => 111,
'child2' => 222
];
$xmlElement = new SimpleXMLElement('<root/>');
$xmlConverter = new Graze\XmlUtils\XmlConverter();
$xmlConverter->addArrayAsChildren($array, $xmlElement);
echo $xmlElement->asXml();
result:
?xml version="1.0"?>
<root><child1>111</child1><child2>222</child2></root>
Attributes are supported by using @attributes
key with an array of attribute name => value.
$array = [
'child1' => [
'@attributes' => [
'id' => 123
]
]
];
result:
<child1 id="123"/>
If a value needs to be set for an element with an attribute then the @value
key should be used.
$array = [
'child1' => [
'@attributes' => [
'id' => 123
],
'@value' => 'some description'
]
];
result:
<child1 id="123">some description</child1>
Repeated elements are supported with indexed arrays.
$array = [
'child' => [
'first',
'second'
]
];
result:
<child>first</child><child>second</child>
$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:
[
'child1' => '123',
'child2' => [
'@attributes' => [
'id' => '1'
],
'subchild1' => '456'
]
];
Formats XML so it is easier to read.
$unformattedXml = '<?xml version="1.0" encoding="UTF-8"?><root><child1>123</child1><child2 attribute="1"><subchild1>456</subchild1></child2></root>';
$xmlFormatter = new XmlFormatter();
$formattedXml = $xmlFormatter->format($unformattedXml);
result:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<child1>123</child1>
<child2 attribute="1">
<subchild1>456</subchild1>
</child2>
</root>
Please see CHANGELOG for more information what has changed recently.
make build test
Please see CONTRIBUTING for details.
If you discover any security related issues, please email security@graze.com instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.