Download the PHP package jackiedo/xml-array without Composer
On this page you can find all versions of the php package jackiedo/xml-array. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download jackiedo/xml-array
More information about jackiedo/xml-array
Files in jackiedo/xml-array
Package xml-array
Short Description Simple tools to work with the conversion between xml and array
License MIT
Informations about the package xml-array
Xml-Array
The conversion between xml and array becomes easier than ever. This package provides some very simple classes to convert XML to array and back.
Features of this package
- Convert an XML object (DOMDocument, SimpleXMLElement) or well-formed XML string to an associative array or Json string.
- Convert an associative array to well-formed XML string or DOMDocument.
- Support parsing and building attributes, Cdata sections and namespaces of XML in conversion process.
Overview
Look at one of the following sessions to learn more about this package.
- Xml-Array
- Features of this package
- Overview
- Installation
- Basic usage
- Convert from XML
- Convert from array
- Advanced usage
- Set configuration
- Method 1
- Method 2
- Method 3
- Get configuration
- Default configuration
- For Xml2Array
- For Array2Xml
- Effect of configuration settings
- version
- encoding
- standalone
- attributesKey, cdataKey, valueKey
- namespacesOnRoot
- rootElement
- keyFixer
- License
Installation
You can install this package through Composer.
Basic usage
Convert from XML
Web have two following methods:
Convert to array:
Convert to Json:
Note: The input XML can be one of types DOMDocument object, SimpleXMLElement object or well-formed XML string.
Example 1: - (Convert from XML string)
After running this piece of code $array will contain:
Example 2: - (Convert form XML object, such as SimpleXMLElement)
The result of above code is:
Example 3: - (Convert to Json)
Convert from array
We also have two following methods:
Convert to XML string:
Convert to DOMDocument object:
Example 4:
Example 5:
Advanced usage
Set configuration
If we want to change the settings for the conversion process, we can do it in the following ways:
Method 1
Note: Configuration is an array of parameters. For more details, see section Default configuration.
Method 2
Method 3
Get configuration
If we implemented the conversion process using methods 2 and 3, we can get configuration of the conversion with method:
Default configuration
For Xml2Array
For Array2Xml
Effect of configuration settings
version
| Use in | Data type |
|---|---|
| Xml2Array, Array2Xml | string |
Effect: This setting allows specifying the XML version to be generated (in Array2Xml), or reconstructed from the XML string (in Xml2Array)
encoding
| Use in | Data type |
|---|---|
| Xml2Array, Array2Xml | string |
Effect: This setting is to indicate the encoding type of the XML to be generated (in Array2Xml), or reconstructed from the XML string (in Xml2Array)
standalone
| Use in | Data type | |
|---|---|---|
| Xml2Array, Array2Xml | null | bool |
Effect: This setting is to allow the standalone directive to appear in the XML or not. If it is set to null, this directive will not appear.
Example:
Content in $xml will be
attributesKey, cdataKey, valueKey
| Use in | Data type |
|---|---|
| Xml2Array, Array2Xml | string |
Effect: This setting allows the use of special keywords to contain the values of properties, CDATA section... during the conversion process.
Example: Please review the examples above for more detailed understanding.
namespacesOnRoot
| Use in | Data type |
|---|---|
| Xml2Array | bool |
Effect: This setting allows to collect all the parsed XML namespaces and place them in the root node. If it is set to false, the namespaces will be located at the nodes where it is declared.
Example: We use the Example 1 above again, but this time the configuration is different:
After running this piece of code $array will contain:
You see, the xlmns:example namespace is put at the example:with_namespace key, not at root_node as it was originally.
rootElement
| Use in | Data type |
|---|---|
| Array2Xml | string |
Effect: According to the Well-formed XML standard, the XML content is only allowed to have a single Root node. This setting allows to wrap all the elements of the original array into a single root node, instead of having to manually edit your array.
keyFixer
| Use in | Data type | |||
|---|---|---|---|---|
| Array2Xml | bool | string | numeric | callable |
Effect:
According to the Well-formed XML standard, the tag names and attributes must satisfy a number of requirements, in which naming is specified as follows:
- Only allowed to start with aphabet characters and underscore.
- Only accept the
[a-zA-Z],-,_,.,:characters. In which, the:is used to indicate the namespace prefix. - Do not allow to end with
:
During conversion, array key names that violate these rules are automatically normalized. If you do not agree to this normalization, set this setting to false.
By default, this normalization replaces invalid characters with underscores (_). You can change to another character as you like.
Example:
License
MIT © Jackie Do
All versions of xml-array with dependencies
ext-dom Version *