You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
'encoding' => 'UTF-8', // Encoding of XML document
445
+
'standalone' => null, // Standalone directive for XML document
438
446
'attributesKey' => '@attributes', // The key name use for storing attributes of node
439
447
'cdataKey' => '@cdata', // The key name use for storing value of Cdata Section in node
440
448
'valueKey' => '@value', // The key name use for storing text content of node
441
-
'namespacesOnRoot' => true // Collapse all the namespaces on the root node, otherwise it will put in the nodes for which the namespace first appeared.
449
+
'namespacesOnRoot' => true // Collapse all the namespaces at the root node, otherwise it will put in the nodes for which the namespace first appeared.
442
450
];
443
451
```
444
452
@@ -448,12 +456,161 @@ $defaultConfig = [
448
456
$defaultConfig = [
449
457
'version' => '1.0', // Version of XML document
450
458
'encoding' => 'UTF-8', // Encoding of XML document
459
+
'standalone' => null, // Standalone directive for XML document
451
460
'attributesKey' => '@attributes', // The key name use for storing attributes of node
452
461
'cdataKey' => '@cdata', // The key name use for storing value of Cdata Section in node
453
462
'valueKey' => '@value', // The key name use for storing text content of node
454
463
'rootElement' => null, // The name of root node will be create automatically in process of conversion
464
+
'keyFixer' => true, // The automatically key normalization will be used during conversion. It can be bool|string|numeric|callable
455
465
];
456
466
```
457
467
468
+
### Effect of configuration settings
469
+
470
+
#### version
471
+
472
+
| Use in | Data type |
473
+
| -------------------- | --------- |
474
+
| Xml2Array, Array2Xml | string |
475
+
476
+
**Effect**: This setting allows specifying the XML version to be generated (in Array2Xml), or reconstructed from the XML string (in Xml2Array)
477
+
478
+
#### encoding
479
+
480
+
| Use in | Data type |
481
+
| -------------------- | --------- |
482
+
| Xml2Array, Array2Xml | string |
483
+
484
+
**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)
485
+
486
+
#### standalone
487
+
488
+
| Use in | Data type |
489
+
| -------------------- | --------- |
490
+
| Xml2Array, Array2Xml | null|bool |
491
+
492
+
**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.
**Effect**: This setting allows the use of special keywords to contain the values of properties, CDATA section... during the conversion process.
516
+
517
+
**Example**: Please review the examples above for more detailed understanding.
518
+
519
+
#### namespacesOnRoot
520
+
521
+
| Use in | Data type |
522
+
| --------- | --------- |
523
+
| Xml2Array | bool |
524
+
525
+
**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.
526
+
527
+
**Example**: We use the `Example 1` above again, but this time the configuration is different:
528
+
529
+
```php
530
+
$array = Xml2Array::convert($xmlString, [
531
+
'namespacesOnRoot' => false
532
+
])->toArray();
533
+
```
534
+
535
+
After running this piece of code `$array` will contain:
536
+
537
+
```php
538
+
$array = [
539
+
"root_node" => [
540
+
"tag" => "Example tag",
541
+
"attribute_tag" => [
542
+
"@value" => "Another tag with attributes",
543
+
"@attributes" => [
544
+
"description" => "This is a tag with attribute"
545
+
]
546
+
],
547
+
...
548
+
"example:with_namespace" => [
549
+
"@attributes" => [
550
+
"xmlns:example" => "http://example.com"
551
+
]
552
+
"example:sub" => "Content"
553
+
],
554
+
]
555
+
]
556
+
```
557
+
558
+
You see, the `xlmns:example` namespace is put at the `example:with_namespace` key, not at `root_node` as it was originally.
559
+
560
+
#### rootElement
561
+
562
+
| Use in | Data type |
563
+
| --------- | --------- |
564
+
| Array2Xml | string |
565
+
566
+
**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.
567
+
568
+
#### keyFixer
569
+
570
+
| Use in | Data type |
571
+
| --------- | ---------------------------- |
572
+
| Array2Xml | bool|string|numeric|callable |
573
+
574
+
**Effect**:
575
+
576
+
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:
577
+
578
+
- Only allowed to start with aphabet characters and underscore.
579
+
- Only accept the `[a-zA-Z]`, `-`, `_`, `.`, `:` characters. In which, the `:` is used to indicate the namespace prefix.
580
+
- Do not allow to end with `:`
581
+
582
+
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`.
583
+
584
+
By default, this normalization replaces invalid characters with underscores (`_`). You can change to another character as you like.
585
+
586
+
**Example**:
587
+
588
+
```php
589
+
// Do not use the key normalization
590
+
$xml = Array2Xml::convert($array, [
591
+
'keyFixer' => false
592
+
])->toXml();
593
+
594
+
// Use the key normalization with default character (_)
0 commit comments