@@ -5,6 +5,8 @@ Convert an array to XML with PHP
5
5
[ ![ License] ( https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square )] ( LICENSE.md )
6
6
[ ![ Build Status] ( https://img.shields.io/travis/refactorstudio/php-array-to-xml/master.svg?style=flat-square )] ( https://travis-ci.org/refactorstudio/php-array-to-xml )
7
7
8
+
9
+
8
10
## Usage
9
11
10
12
Basic example:
@@ -21,3 +23,337 @@ Output:
21
23
<?xml version =" 1.0" encoding =" UTF-8" ?>
22
24
<root ><title >My Products</title ></root >
23
25
```
26
+
27
+
28
+
29
+ ## Output Format (Prettify)
30
+ > ` ->setFormatOutput(bool $value = false) `
31
+
32
+ > Alias: ` ->prettify() ` is the same as typing: ` ->setFormatOutput(true) `
33
+
34
+ ``` php
35
+ $array = [
36
+ 'title' => 'My Products',
37
+ 'pricing' => 'Pricing'
38
+ ];
39
+ ```
40
+
41
+ Default:
42
+ ``` xml
43
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
44
+ <root ><title >My Products</title ><pricing >Pricing</pricing ></root >
45
+ ```
46
+
47
+ Usage:
48
+ ``` php
49
+ $result = $converter->setFormatOutput(true)->toXmlString($array);
50
+
51
+ // or use the alias:
52
+ $result = $converter->prettify()->toXmlString($array);
53
+ ```
54
+
55
+ Result:
56
+ ``` xml
57
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
58
+ <root >
59
+ <title >My Products</title >
60
+ <pricing >Pricing</pricing >
61
+ </root >
62
+ ```
63
+
64
+
65
+
66
+ ## Custom root name
67
+ > ` ->setCustomRootName(string $value = 'root') `
68
+
69
+ ``` php
70
+ $result = $converter->setCustomRootName('data')->toXmlString();
71
+ ```
72
+
73
+ Result:
74
+ ``` xml
75
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
76
+ <data >
77
+ ...
78
+ </data >
79
+ ```
80
+
81
+
82
+
83
+ ## Custom tag name
84
+ > Custom tag names are used when an array has no key names
85
+ >
86
+ > ` ->setCustomTagName(string $value = 'node') `
87
+
88
+ ``` php
89
+ $array = [
90
+ 'title' => 'My Products',
91
+ 'products' => [
92
+ [
93
+ 'name' => 'Raspberry Pi 3',
94
+ 'price' => 39.99
95
+ ],
96
+ [
97
+ 'name' => 'Arduino Uno Rev3',
98
+ 'price' => 19.99
99
+ ]
100
+ ]
101
+ ];
102
+ ```
103
+
104
+ Default (prettified):
105
+ ``` xml
106
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
107
+ <root >
108
+ <title >My Products</title >
109
+ <products >
110
+ <node >
111
+ <name >Raspberry Pi 3</name >
112
+ <price >39.99</price >
113
+ </node >
114
+ <node >
115
+ <name >Arduino Uno Rev3</name >
116
+ <price >19.99</price >
117
+ </node >
118
+ </products >
119
+ </root >
120
+ ```
121
+
122
+ Usage:
123
+ ``` php
124
+ $xml_string = $converter->setCustomTagName('item')->toXmlString($array);
125
+ ```
126
+
127
+ Result (prettified):
128
+ ``` xml
129
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
130
+ <root >
131
+ <title >My Products</title >
132
+ <products >
133
+ <item >
134
+ <name >Raspberry Pi 3</name >
135
+ <price >39.99</price >
136
+ </item >
137
+ <item >
138
+ <name >Arduino Uno Rev3</name >
139
+ <price >19.99</price >
140
+ </item >
141
+ </products >
142
+ </root >
143
+ ```
144
+
145
+
146
+
147
+ ## XML version
148
+ > ` ->setVersion(string $value = '1.0') `
149
+
150
+ ``` php
151
+ $xml_string = $converter->setVersion('1.1')->toXmlString(['test']);
152
+ ```
153
+
154
+ Result (prettified):
155
+ ``` xml
156
+ <?xml version =" 1.1" encoding =" UTF-8" ?>
157
+ <root >
158
+ <node >test</node >
159
+ </root >
160
+ ```
161
+
162
+
163
+
164
+ ## XML encoding
165
+ > ` ->setEncoding(string $value = 'UTF-8') `
166
+
167
+ ``` php
168
+ $xml_string = $converter->setEncoding('ISO-8859-1')->toXmlString(['test']);
169
+ ```
170
+
171
+ Result (prettified):
172
+ ``` xml
173
+ <?xml version =" 1.0" encoding =" ISO-8859-1" ?>
174
+ <root >
175
+ <node >test</node >
176
+ </root >
177
+ ```
178
+
179
+
180
+
181
+ ## Tag separator
182
+ > Set the value for the separator that will be used to replace special characters in tag names
183
+ >
184
+ > ` ->setSeparator(string $value = '_') `
185
+
186
+ ``` php
187
+ $array = [
188
+ 'some of these keys have' => 'My Value 1',
189
+ 'spaces in them' => 'My Value 2',
190
+ ];
191
+ ```
192
+
193
+ Default (prettified):
194
+ ``` xml
195
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
196
+ <root >
197
+ <some_of_these_keys_have >My Value 1</some_of_these_keys_have >
198
+ <spaces_in_them >My Value 2</spaces_in_them >
199
+ </root >
200
+ ```
201
+
202
+ Usage:
203
+ ``` php
204
+ $xml_string = $converter->setSeparator('-')->toXmlString($array);
205
+ ```
206
+
207
+ Result (prettified):
208
+ ``` xml
209
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
210
+ <root >
211
+ <some-of-these-keys-have >My Value 1</some-of-these-keys-have >
212
+ <spaces-in-them >My Value 2</spaces-in-them >
213
+ </root >
214
+ ```
215
+
216
+
217
+
218
+ ## Transform tag names
219
+ > Transform tag names to uppercase/lowercase
220
+ >
221
+ > ` ->setTransformTags(string $value = null) `
222
+
223
+ ``` php
224
+ $array = [
225
+ 'This' => [
226
+ 'Is' => [
227
+ 'an',
228
+ 'Example'
229
+ ]
230
+ ]
231
+ ];
232
+ ```
233
+
234
+ Default (prettified):
235
+ ``` xml
236
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
237
+ <root >
238
+ <This >
239
+ <Is >
240
+ <node >an</node >
241
+ <node >Example</node >
242
+ </Is >
243
+ </This >
244
+ </root >
245
+ ```
246
+
247
+ Usage (lowercase):
248
+ ``` php
249
+ $xml_string = $converter->setTransformTags('lowercase')->toXmlString($array);
250
+ ```
251
+
252
+ Result (prettified):
253
+ ``` xml
254
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
255
+ <root >
256
+ <this >
257
+ <is >
258
+ <node >an</node >
259
+ <node >Example</node >
260
+ </is >
261
+ </this >
262
+ </root >
263
+ ```
264
+
265
+ Usage (uppercase):
266
+ ``` php
267
+ $xml_string = $converter->setTransformTags('uppercase')->toXmlString($array);
268
+ ```
269
+
270
+ Result (prettified):
271
+ ``` xml
272
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
273
+ <ROOT >
274
+ <THIS >
275
+ <IS >
276
+ <NODE >an</NODE >
277
+ <NODE >Example</NODE >
278
+ </IS >
279
+ </THIS >
280
+ </ROOT >
281
+ ```
282
+
283
+
284
+ Usage (uppercase, but with custom tag names, which will not be transformed):
285
+ ``` php
286
+ $xml_string = $converter
287
+ ->setTransformTags('uppercase')
288
+ ->setCustomRootName('MyRoot')
289
+ ->setCustomTagName('MyCustomTag')
290
+ ->toXmlString($array);
291
+ ```
292
+
293
+ Result (prettified):
294
+ ``` xml
295
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
296
+ <MyRoot >
297
+ <THIS >
298
+ <IS >
299
+ <MyCustomTag >an</MyCustomTag >
300
+ <MyCustomTag >Example</MyCustomTag >
301
+ </IS >
302
+ </THIS >
303
+ </MyRoot >
304
+ ```
305
+
306
+
307
+
308
+ ## Set numeric tag suffix
309
+ > If this is not null, it appends the numeric array key to the tag name, with the value as separator.
310
+ >
311
+ > ` ->setNumericTagSuffix(string $value = null) `
312
+
313
+ ``` php
314
+ $array = [
315
+ 'this',
316
+ 'is',
317
+ 'an'
318
+ [
319
+ 'example',
320
+ 'using',
321
+ 'numeric tag suffix',
322
+ ],
323
+ ];
324
+ ```
325
+
326
+ Default (prettified):
327
+ ``` xml
328
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
329
+ <root >
330
+ <node >this</node >
331
+ <node >is</node >
332
+ <node >an</node >
333
+ <node >
334
+ <node >example</node >
335
+ <node >using</node >
336
+ <node >numeric tag suffix</node >
337
+ </node >
338
+ </root >
339
+ ```
340
+
341
+ Usage:
342
+ ``` php
343
+ $xml_string = $converter->setNumericTagSuffix('_')->toXmlString($array);
344
+ ```
345
+
346
+ Result (prettified):
347
+ ``` xml
348
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
349
+ <root >
350
+ <node_0 >this</node_0 >
351
+ <node_1 >is</node_1 >
352
+ <node_2 >an</node_2 >
353
+ <node_3 >
354
+ <node_0 >example</node_0 >
355
+ <node_1 >using</node_1 >
356
+ <node_2 >numeric tag suffix</node_2 >
357
+ </node_3 >
358
+ </root >
359
+ ```
0 commit comments