Download the PHP package spatie/array-to-xml without Composer
On this page you can find all versions of the php package spatie/array-to-xml. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download spatie/array-to-xml
More information about spatie/array-to-xml
Files in spatie/array-to-xml
Package array-to-xml
Short Description Convert an array to xml
License MIT
Homepage https://github.com/spatie/array-to-xml
Informations about the package array-to-xml
Convert an array to xml
This package provides a very simple class to convert an array to an xml string.
Support us
We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.
We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.
Install
You can install this package via composer.
Usage
After running this piece of code $result
will contain:
Setting the name of the root element
Optionally you can set the name of the rootElement by passing it as the second argument. If you don't specify this argument (or set it to an empty string) "root" will be used.
Handling key names
By default all spaces in the key names of your array will be converted to underscores. If you want to opt out of this behaviour you can set the third argument to false. We'll leave all keynames alone.
Adding attributes
You can use a key named _attributes
to add attributes to a node, and _value
to specify the value.
This code will result in:
Note, that the value of the _value
field must be a string. (More)
Using reserved characters
It is also possible to wrap the value of a node into a CDATA section. This allows you to use reserved characters.
This code will result in:
If your input contains something that cannot be parsed a DOMException
will be thrown.
Customize the XML declaration
You could specify specific values in for:
- encoding as the fourth argument (string)
- version as the fifth argument (string)
- DOM properties as the sixth argument (array)
- standalone as seventh argument (boolean)
This will result in:
Adding attributes to the root element
To add attributes to the root element provide an array with an _attributes
key as the second argument.
The root element name can then be set using the rootElementName
key.
Using a multi-dimensional array
Use a multi-dimensional array to create a collection of elements.
This will result in:
Using Closure values
The package can use Closure values:
This will result in:
Handling numeric keys
The package can also can handle numeric keys:
This will result in:
You can change key prefix with setter method called setNumericTagNamePrefix()
.
Using custom keys
The package can also can handle custom keys:
This will result in:
A custom key contains three, colon-separated parts: "__custom:[custom-tag]:[unique-string]".
- "__custom"
- The key always starts with "__custom".
- [custom-tag]
- The string to be rendered as the XML tag.
- [unique-string]
- A unique string that avoids overwriting of duplicate keys in PHP arrays.
a colon character can be included within the custom-tag portion by escaping it with a backslash:
This will result in:
Setting DOMDocument properties
To set properties of the internal DOMDocument object just pass an array consisting of keys and values. For a full list of valid properties consult https://www.php.net/manual/en/class.domdocument.php.
You can use the constructor to set DOMDocument properties.
Alternatively you can use setDomProperties
XML Prettification
Call $arrayToXml->prettify()
method on ArrayToXml to set XML in pretty form.
Example:
With prettification:
will result in:
Without prettification:
will result in:
Dropping XML declaration
Call $arrayToXml->dropXmlDeclaration()
method on ArrayToXml object to omit default XML declaration on top of the generated XML.
Example:
This will result in:
Adding processing instructions
Call $arrayToXml->addProcessingInstruction($target, $data)
method on ArrayToXml object to prepend a processing instruction before the root element.
Example:
This will result in:
Testing
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Postcardware
You're free to use this package, but if it makes it to your production environment we highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using.
Our address is: Spatie, Kruikstraat 22, 2018 Antwerp, Belgium.
We publish all received postcards on our company website.
Credits
- Freek Van der Herten
- All Contributors
License
The MIT License (MIT). Please see License File for more information.
All versions of array-to-xml with dependencies
ext-dom Version *