@@ -51,19 +51,25 @@ After running this piece of code `$result` will contain:
51
51
</root >
52
52
```
53
53
54
+ ### Setting the name of the root element
55
+
54
56
Optionally you can set the name of the rootElement by passing it as the second argument. If you don't specify
55
57
this argument (or set it to an empty string) "root" will be used.
56
58
```
57
59
$result = ArrayToXml::convert($array, 'customrootname');
58
60
```
59
61
62
+ ### Handling key names
63
+
60
64
By default all spaces in the key names of your array will be converted to underscores. If you want to opt out of
61
65
this behaviour you can set the third argument to false. We'll leave all keynames alone.
62
66
```
63
67
$result = ArrayToXml::convert($array, 'customrootname', false);
64
68
```
65
69
66
- You can use a key named ` _attributes ` to add attributes to a node.
70
+ ### Adding attributes
71
+
72
+ You can use a key named ` _attributes ` to add attributes to a node, and ` _value ` to specify the value.
67
73
68
74
``` php
69
75
$array = [
@@ -76,6 +82,10 @@ $array = [
76
82
'name' => 'Sauron',
77
83
'weapon' => 'Evil Eye'
78
84
]
85
+ 'The survivor' => [
86
+ '_attributes' => ['house'=>'Hogwarts'],
87
+ '_value' => 'Harry Potter'
88
+ ]
79
89
];
80
90
81
91
$result = ArrayToXml::convert($array);
@@ -94,9 +104,14 @@ This code will result in:
94
104
<name >Sauron</name >
95
105
<weapon >Evil Eye</weapon >
96
106
</Bad_guy >
107
+ <The_survivor house =" Hogwarts" >
108
+ Harry Potter
109
+ </The_survivor >
97
110
</root >
98
111
```
99
112
113
+ ### Using reserved characters
114
+
100
115
It is also possible to wrap the value of a node into a CDATA section. This allows you to use reserved characters.
101
116
102
117
``` php
@@ -134,6 +149,8 @@ This code will result in:
134
149
135
150
If your input contains something that cannot be parsed a ` DOMException ` will be thrown.
136
151
152
+ ### Adding attributes to the root element
153
+
137
154
To add attributes to the root element provide an array with an ` _attributes ` key as the second argument.
138
155
The root element name can then be set using the ` rootElementName ` key.
139
156
@@ -143,7 +160,118 @@ $result = ArrayToXml::convert($array, [
143
160
'_attributes' => [
144
161
'xmlns' => 'https://github.com/spatie/array-to-xml',
145
162
],
146
- ]);
163
+ ], true, 'UTF-8');
164
+ ```
165
+
166
+ ### Using a multi-dimensional array
167
+
168
+ Use a multi-dimensional array to create a collection of elements.
169
+ ``` php
170
+ $array = [
171
+ 'Good guys' => [
172
+ 'Guy' => [
173
+ ['name' => 'Luke Skywalker', 'weapon' => 'Lightsaber'],
174
+ ['name' => 'Captain America', 'weapon' => 'Shield'],
175
+ ],
176
+ ],
177
+ 'Bad guys' => [
178
+ 'Guy' => [
179
+ ['name' => 'Sauron', 'weapon' => 'Evil Eye'],
180
+ ['name' => 'Darth Vader', 'weapon' => 'Lightsaber'],
181
+ ],
182
+ ],
183
+ ];
184
+ ```
185
+
186
+ This will result in:
187
+
188
+ ``` xml
189
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
190
+ <helloyouluckypeople xmlns =" https://github.com/spatie/array-to-xml" >
191
+ <Good_guys >
192
+ <Guy >
193
+ <name >Luke Skywalker</name >
194
+ <weapon >Lightsaber</weapon >
195
+ </Guy >
196
+ <Guy >
197
+ <name >Captain America</name >
198
+ <weapon >Shield</weapon >
199
+ </Guy >
200
+ </Good_guys >
201
+ <Bad_guys >
202
+ <Guy >
203
+ <name >Sauron</name >
204
+ <weapon >Evil Eye</weapon >
205
+ </Guy >
206
+ <Guy >
207
+ <name >Darth Vader</name >
208
+ <weapon >Lightsaber</weapon >
209
+ </Guy >
210
+ </Bad_guys >
211
+ </helloyouluckypeople >
212
+ ```
213
+
214
+ ### Handling numeric keys
215
+
216
+ The package can also can handle numeric keys:
217
+
218
+ ``` php
219
+ $array = [
220
+ 100 => [
221
+ 'name' => 'Vladimir',
222
+ 'nickname' => 'greeflas',
223
+ ],
224
+ 200 => [
225
+ 'name' => 'Marina',
226
+ 'nickname' => 'estacet',
227
+ ],
228
+ ];
229
+
230
+ $result = ArrayToXml::convert(['__numeric' => $array]);
231
+ ```
232
+
233
+ This will result in:
234
+
235
+ ``` xml
236
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
237
+ <root >
238
+ <numeric_100 >
239
+ <name >Vladimir</name >
240
+ <nickname >greeflas</nickname >
241
+ </numeric_100 >
242
+ <numeric_200 >
243
+ <name >Marina</name >
244
+ <nickname >estacet</nickname >
245
+ </numeric_200 >
246
+ </root >
247
+ ```
248
+
249
+ You can change key prefix with setter method called ` setNumericTagNamePrefix() ` .
250
+
251
+ ### Setting DOMDocument properties
252
+
253
+ 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 .
254
+
255
+ You can use the constructor to set DOMDocument properties.
256
+
257
+ ``` php
258
+ $result = ArrayToXml::convert(
259
+ $array,
260
+ $rootElement,
261
+ $replaceSpacesByUnderScoresInKeyNames,
262
+ $xmlEncoding,
263
+ $xmlVersion,
264
+ ['formatOutput' => true]
265
+ );
266
+
267
+ ```
268
+
269
+ Alternatively you can use ` setDomProperties `
270
+
271
+ ``` php
272
+ $arrayToXml = new ArrayToXml($array);
273
+ $arrayToXml->setDomProperties(['formatOutput' => true]);
274
+ $result = $arrayToXml->toXml();
147
275
```
148
276
149
277
## Testing
0 commit comments