|
1 | | -# Xml-Array |
| 1 | +# Xml Array |
| 2 | +[](https://packagist.org/packages/jackiedo/xml-array) |
| 3 | +[](https://packagist.org/packages/jackiedo/xml-array) |
| 4 | +[](https://packagist.org/packages/jackiedo/xml-array) |
| 5 | +[](https://packagist.org/packages/jackiedo/xml-array) |
| 6 | + |
| 7 | +The conversion between xml and array becomes easier than ever. This package provides some very simple classes to convert XML to array and back. |
| 8 | + |
| 9 | +# Features of this package |
| 10 | +* Convert an XML object (DOMDocument, SimpleXMLElement) or well-formed XML string to an associative array or Json string. |
| 11 | +* Convert an associative array to well-formed XML string or DOMDocument. |
| 12 | +* Support parsing and building attributes, Cdata sections and namespaces of XML in conversion process. |
| 13 | + |
| 14 | +# Overview |
| 15 | +Look at one of the following sessions to learn more about this package. |
| 16 | + |
| 17 | +* [Installation](#installation) |
| 18 | +* [Basic usage](#basic-usage) |
| 19 | + - [Convert XML to array](#convert-xml-to-array) |
| 20 | + - [Convert XML to Json](#convert-xml-to-json) |
| 21 | + - [Convert array to XML](#convert-array-to-xml) |
| 22 | + - [Convert array to DOM](#convert-array-to-dom) |
| 23 | +* [Advanced usage](#advanced-usage) |
| 24 | + - [Set configuration](#set-configuration) |
| 25 | + - [Get configuration](#get-configuration) |
| 26 | + - [Default configuration](#default-configuration) |
| 27 | +* [License](#license) |
| 28 | + |
| 29 | +## Installation |
| 30 | +You can install this package through [Composer](https://getcomposer.org). |
| 31 | + |
| 32 | +```shell |
| 33 | +composer require jackiedo/xml-array |
| 34 | +``` |
| 35 | + |
| 36 | +## Basic usage |
| 37 | + |
| 38 | +### Convert XML to array |
| 39 | + |
| 40 | +###### Syntax: |
| 41 | + |
| 42 | +``` |
| 43 | +array Xml2Array::convert(DOMDocument|SimpleXMLElement|string $inputXML)->toArray(); |
| 44 | +``` |
| 45 | + |
| 46 | +> **Note:** The input XML can be one of types DOMDocument object, SimpleXMLElement object or well-formed XML string. |
| 47 | +
|
| 48 | +###### Example: |
| 49 | + |
| 50 | +```php |
| 51 | +use Jackiedo\XmlArray\Xml2Array; |
| 52 | +... |
| 53 | + |
| 54 | +$xmlString = '<?xml version="1.0" encoding="UTF-8"?> |
| 55 | + <root_node> |
| 56 | + <tag>Example tag</tag> |
| 57 | + |
| 58 | + <attribute_tag description="This is a tag with attribute">Another tag with attributes</attribute_tag> |
| 59 | + |
| 60 | + <cdata_section><![CDATA[ This is CDATA section ]]></cdata_section> |
| 61 | + |
| 62 | + <tag_with_subtag> |
| 63 | + <sub_tag>Sub tag 1</sub_tag> |
| 64 | + <sub_tag>Sub tag 2</sub_tag> |
| 65 | + </tag_with_subtag> |
| 66 | + |
| 67 | + <mixed_section> |
| 68 | + Hello |
| 69 | + |
| 70 | + <![CDATA[ This is another CDATA section ]]> |
| 71 | + |
| 72 | + <section id="sec_1">Section number 1</section> |
| 73 | + <section id="sec_2">Section number 2</section> |
| 74 | + <section id="sec_3">Section number 3</section> |
| 75 | + </mixed_section> |
| 76 | + |
| 77 | + <example:with_namespace xmlns:example="http://example.com"> |
| 78 | + <example:sub>Content</example:sub> |
| 79 | + </example:with_namespace> |
| 80 | + </root_node>'; |
| 81 | + |
| 82 | +$array = Xml2Array::convert($xmlString)->toArray(); |
| 83 | +``` |
| 84 | + |
| 85 | +After running this piece of code `$array` will contain: |
| 86 | + |
| 87 | +```php |
| 88 | +$array = [ |
| 89 | + "root_node" => [ |
| 90 | + "tag" => "Example tag", |
| 91 | + "attribute_tag" => [ |
| 92 | + "@value" => "Another tag with attributes", |
| 93 | + "@attributes" => [ |
| 94 | + "description" => "This is a tag with attribute" |
| 95 | + ] |
| 96 | + ], |
| 97 | + "cdata_section" => [ |
| 98 | + "@cdata" => "This is CDATA section" |
| 99 | + ], |
| 100 | + "tag_with_subtag" => [ |
| 101 | + "sub_tag" => ["Sub tag 1", "Sub tag 2"] |
| 102 | + ], |
| 103 | + "mixed_section" => [ |
| 104 | + "@value" => "Hello", |
| 105 | + "@cdata" => "This is another CDATA section", |
| 106 | + "section" => [ |
| 107 | + [ |
| 108 | + "@value" => "Section number 1", |
| 109 | + "@attributes" => [ |
| 110 | + "id" => "sec_1" |
| 111 | + ] |
| 112 | + ], |
| 113 | + [ |
| 114 | + "@value" => "Section number 2", |
| 115 | + "@attributes" => [ |
| 116 | + "id" => "sec_2" |
| 117 | + ] |
| 118 | + ], |
| 119 | + [ |
| 120 | + "@value" => "Section number 3", |
| 121 | + "@attributes" => [ |
| 122 | + "id" => "sec_3" |
| 123 | + ] |
| 124 | + ] |
| 125 | + ] |
| 126 | + ], |
| 127 | + "example:with_namespace" => [ |
| 128 | + "example:sub" => "Content" |
| 129 | + ], |
| 130 | + "@attributes" => [ |
| 131 | + "xmlns:example" => "http://example.com" |
| 132 | + ] |
| 133 | + ] |
| 134 | +] |
| 135 | +``` |
| 136 | + |
| 137 | +### Convert XML to Json |
| 138 | + |
| 139 | +###### Syntax: |
| 140 | + |
| 141 | +``` |
| 142 | +string Xml2Array::convert(DOMDocument|SimpleXMLElement|string $inputXML)->toJson([int $options = 0]); |
| 143 | +``` |
| 144 | + |
| 145 | +###### Example: |
| 146 | + |
| 147 | +```php |
| 148 | +$jsonString = Xml2Array::convert($xmlString)->toJson(JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT); |
| 149 | +``` |
| 150 | + |
| 151 | +### Convert array to XML |
| 152 | + |
| 153 | +###### Syntax: |
| 154 | + |
| 155 | +``` |
| 156 | +string Array2Xml::convert(array $array)->toXml([bool $prettyOutput = false]); |
| 157 | +``` |
| 158 | + |
| 159 | +###### Example: |
| 160 | + |
| 161 | +```php |
| 162 | +use Jackiedo\XmlArray\Array2Xml; |
| 163 | +... |
| 164 | + |
| 165 | +// We will use array from the result of above example as input for this example |
| 166 | +$xmlString = Array2Xml::convert($array)->toXml(true); |
| 167 | +``` |
| 168 | + |
| 169 | +### Convert array to DOM |
| 170 | + |
| 171 | +###### Syntax: |
| 172 | + |
| 173 | +``` |
| 174 | +DOMDocument Array2Xml::convert(array $array)->toDom(); |
| 175 | +``` |
| 176 | + |
| 177 | +###### Example: |
| 178 | + |
| 179 | +```php |
| 180 | +$domObject = Array2Xml::convert($array)->toDom(); |
| 181 | +``` |
| 182 | + |
| 183 | +## Advanced usage |
| 184 | + |
| 185 | +### Set configuration |
| 186 | +You can set configuration for conversion process with one of following methods: |
| 187 | + |
| 188 | +###### Method 1: |
| 189 | + |
| 190 | +```php |
| 191 | +... |
| 192 | +$config = [ |
| 193 | + 'valueKey' => '@text', |
| 194 | + 'cdataKey' => '@cdata-section' |
| 195 | +]; |
| 196 | + |
| 197 | +$array = Xml2Array::convert($inputXml, $config)->toArray(); |
| 198 | +... |
| 199 | + |
| 200 | +// And for backward processing |
| 201 | +$xml = Array2Xml::convert($inputArray, $config)->toXml(); |
| 202 | +``` |
| 203 | + |
| 204 | +> **Note**: Configuration is an array of parameters. For more details, see section [Default configuration](#default-configuration). |
| 205 | +
|
| 206 | +###### Method 2: |
| 207 | + |
| 208 | +```php |
| 209 | +$converter = new Xml2Array($config); |
| 210 | +$array = $converter->convertFrom($inputXml)->toArray(); |
| 211 | +``` |
| 212 | + |
| 213 | +###### Method 3: |
| 214 | + |
| 215 | +```php |
| 216 | +$converter = new Xml2Array; |
| 217 | +$array = $converter->setConfig($config)->convertFrom($inputXml)->toArray(); |
| 218 | +``` |
| 219 | + |
| 220 | +### Get configuration |
| 221 | +If you implemented the conversion process using methods 2 and 3, you can get configuration of the conversion with method: |
| 222 | + |
| 223 | +```php |
| 224 | +$config = $converter->getConfig(); |
| 225 | +``` |
| 226 | + |
| 227 | +### Default configuration |
| 228 | + |
| 229 | +###### For Xml2Array |
| 230 | + |
| 231 | +```php |
| 232 | +$defaultConfig = [ |
| 233 | + 'version' => '1.0', // Version of XML document |
| 234 | + 'encoding' => 'UTF-8', // Encoding of XML document |
| 235 | + 'attributesKey' => '@attributes', // The key name use for storing attributes of node |
| 236 | + 'cdataKey' => '@cdata', // The key name use for storing value of Cdata Section in node |
| 237 | + 'valueKey' => '@value', // The key name use for storing text content of node |
| 238 | + 'namespacesOnRoot' => true // Collapse all the namespaces on the root node, otherwise it will put in the nodes for which the namespace first appeared. |
| 239 | +]; |
| 240 | +``` |
| 241 | + |
| 242 | +###### For Array2Xml |
| 243 | + |
| 244 | +```php |
| 245 | +$defaultConfig = [ |
| 246 | + 'version' => '1.0', // Version of XML document |
| 247 | + 'encoding' => 'UTF-8', // Encoding of XML document |
| 248 | + 'attributesKey' => '@attributes', // The key name use for storing attributes of node |
| 249 | + 'cdataKey' => '@cdata', // The key name use for storing value of Cdata Section in node |
| 250 | + 'valueKey' => '@value', // The key name use for storing text content of node |
| 251 | + 'rootElement' => null, // The name of root node will be create automatically in process of conversion |
| 252 | +]; |
| 253 | +``` |
| 254 | + |
| 255 | +## License |
| 256 | +[MIT](LICENSE) © Jackie Do |
0 commit comments