[ Index ]

PHP Cross Reference of YOURLS

title

Body

[close]

/includes/vendor/spatie/array-to-xml/ -> README.md (source)

   1  
   2  [<img src="https://github-ads.s3.eu-central-1.amazonaws.com/support-ukraine.svg?t=1" />](https://supportukrainenow.org)
   3  
   4  # Convert an array to xml
   5  
   6  [![Latest Version](https://img.shields.io/github/release/spatie/array-to-xml.svg?style=flat-square)](https://github.com/spatie/array-to-xml/releases)
   7  [![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md)
   8  ![Tests](https://github.com/spatie/array-to-xml/workflows/Tests/badge.svg)
   9  [![Total Downloads](https://img.shields.io/packagist/dt/spatie/array-to-xml.svg?style=flat-square)](https://packagist.org/packages/spatie/array-to-xml)
  10  
  11  This package provides a very simple class to convert an array to an xml string.
  12  
  13  ## Support us
  14  
  15  [<img src="https://github-ads.s3.eu-central-1.amazonaws.com/array-to-xml.jpg?t=1" width="419px" />](https://spatie.be/github-ad-click/array-to-xml)
  16  
  17  We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us).
  18  
  19  We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards).
  20  
  21  ## Install
  22  
  23  You can install this package via composer.
  24  
  25  ``` bash
  26  composer require spatie/array-to-xml
  27  ```
  28  
  29  ## Usage
  30  
  31  ```php
  32  use Spatie\ArrayToXml\ArrayToXml;
  33  ...
  34  $array = [
  35      'Good guy' => [
  36          'name' => 'Luke Skywalker',
  37          'weapon' => 'Lightsaber'
  38      ],
  39      'Bad guy' => [
  40          'name' => 'Sauron',
  41          'weapon' => 'Evil Eye'
  42      ]
  43  ];
  44  
  45  $result = ArrayToXml::convert($array);
  46  ```
  47  After running this piece of code `$result` will contain:
  48  
  49  ```xml
  50  <?xml version="1.0"?>
  51  <root>
  52      <Good_guy>
  53          <name>Luke Skywalker</name>
  54          <weapon>Lightsaber</weapon>
  55      </Good_guy>
  56      <Bad_guy>
  57          <name>Sauron</name>
  58          <weapon>Evil Eye</weapon>
  59      </Bad_guy>
  60  </root>
  61  ```
  62  
  63  
  64  ### Setting the name of the root element
  65  
  66  Optionally you can set the name of the rootElement by passing it as the second argument. If you don't specify
  67  this argument (or set it to an empty string) "root" will be used.
  68  ```
  69  $result = ArrayToXml::convert($array, 'customrootname');
  70  ```
  71  
  72  ### Handling key names
  73  
  74  By default all spaces in the key names of your array will be converted to underscores. If you want to opt out of
  75  this behaviour you can set the third argument to false. We'll leave all keynames alone.
  76  ```
  77  $result = ArrayToXml::convert($array, 'customrootname', false);
  78  ```
  79  
  80  ### Adding attributes
  81  
  82  You can use a key named `_attributes` to add attributes to a node, and `_value` to specify the value.
  83  
  84  ```php
  85  $array = [
  86      'Good guy' => [
  87          '_attributes' => ['attr1' => 'value'],
  88          'name' => 'Luke Skywalker',
  89          'weapon' => 'Lightsaber'
  90      ],
  91      'Bad guy' => [
  92          'name' => 'Sauron',
  93          'weapon' => 'Evil Eye'
  94      ],
  95      'The survivor' => [
  96          '_attributes' => ['house'=>'Hogwarts'],
  97          '_value' => 'Harry Potter'
  98      ]
  99  ];
 100  
 101  $result = ArrayToXml::convert($array);
 102  ```
 103  
 104  This code will result in:
 105  
 106  ```xml
 107  <?xml version="1.0"?>
 108  <root>
 109      <Good_guy attr1="value">
 110          <name>Luke Skywalker</name>
 111          <weapon>Lightsaber</weapon>
 112      </Good_guy>
 113      <Bad_guy>
 114          <name>Sauron</name>
 115          <weapon>Evil Eye</weapon>
 116      </Bad_guy>
 117      <The_survivor house="Hogwarts">
 118          Harry Potter
 119      </The_survivor>
 120  </root>
 121  ```
 122  
 123  *Note, that the value of the `_value` field must be a string. [(More)](https://github.com/spatie/array-to-xml/issues/75#issuecomment-413726065)* 
 124  
 125  ### Using reserved characters
 126  
 127  It is also possible to wrap the value of a node into a CDATA section. This allows you to use reserved characters.
 128  
 129  ```php
 130  $array = [
 131      'Good guy' => [
 132          'name' => [
 133              '_cdata' => '<h1>Luke Skywalker</h1>'
 134          ],
 135          'weapon' => 'Lightsaber'
 136      ],
 137      'Bad guy' => [
 138          'name' => '<h1>Sauron</h1>',
 139          'weapon' => 'Evil Eye'
 140      ]
 141  ];
 142  
 143  $result = ArrayToXml::convert($array);
 144  ```
 145  
 146  This code will result in:
 147  
 148  ```xml
 149  <?xml version="1.0"?>
 150  <root>
 151      <Good_guy>
 152          <name><![CDATA[<h1>Luke Skywalker</h1>]]></name>
 153          <weapon>Lightsaber</weapon>
 154      </Good_guy>
 155      <Bad_guy>
 156          <name>&lt;h1&gt;Sauron&lt;/h1&gt;</name>
 157          <weapon>Evil Eye</weapon>
 158      </Bad_guy>
 159  </root>
 160  ```
 161  
 162  If your input contains something that cannot be parsed a `DOMException` will be thrown.
 163  
 164  
 165  ### Customize the XML declaration
 166  
 167  You could specify specific values in for:
 168   - encoding as the fourth argument (string)
 169   - version as the fifth argument (string)
 170   - standalone as sixth argument (boolean)
 171  
 172  ```php
 173  $result = ArrayToXml::convert($array, [], true, 'UTF-8', '1.1', [], true);
 174  ```
 175  
 176  This will result in:
 177  
 178  ```xml
 179  <?xml version="1.1" encoding="UTF-8" standalone="yes"?>
 180  ```
 181  
 182  
 183  ### Adding attributes to the root element
 184  
 185  To add attributes to the root element provide an array with an `_attributes` key as the second argument. 
 186  The root element name can then be set using the `rootElementName` key.
 187  
 188  ```php
 189  $result = ArrayToXml::convert($array, [
 190      'rootElementName' => 'helloyouluckypeople',
 191      '_attributes' => [
 192          'xmlns' => 'https://github.com/spatie/array-to-xml',
 193      ],
 194  ], true, 'UTF-8');
 195  ```
 196  
 197  ### Using a multi-dimensional array
 198  
 199  Use a multi-dimensional array to create a collection of elements.
 200  ```php
 201  $array = [
 202      'Good guys' => [
 203          'Guy' => [
 204              ['name' => 'Luke Skywalker', 'weapon' => 'Lightsaber'],
 205              ['name' => 'Captain America', 'weapon' => 'Shield'],
 206          ],
 207      ],
 208      'Bad guys' => [
 209          'Guy' => [
 210              ['name' => 'Sauron', 'weapon' => 'Evil Eye'],
 211              ['name' => 'Darth Vader', 'weapon' => 'Lightsaber'],
 212          ],
 213      ],
 214  ];
 215  ```
 216  
 217  This will result in:
 218  
 219  ```xml
 220  <?xml version="1.0" encoding="UTF-8"?>
 221  <helloyouluckypeople xmlns="https://github.com/spatie/array-to-xml">
 222      <Good_guys>
 223          <Guy>
 224              <name>Luke Skywalker</name>
 225              <weapon>Lightsaber</weapon>
 226          </Guy>
 227          <Guy>
 228              <name>Captain America</name>
 229              <weapon>Shield</weapon>
 230          </Guy>
 231      </Good_guys>
 232      <Bad_guys>
 233          <Guy>
 234              <name>Sauron</name>
 235              <weapon>Evil Eye</weapon>
 236          </Guy>
 237          <Guy>
 238              <name>Darth Vader</name>
 239              <weapon>Lightsaber</weapon>
 240          </Guy>
 241      </Bad_guys>
 242  </helloyouluckypeople>
 243  ```
 244  
 245  ### Handling numeric keys
 246  
 247  The package can also can handle numeric keys:
 248  
 249  ```php
 250  $array = [
 251      100 => [
 252          'name' => 'Vladimir',
 253          'nickname' => 'greeflas',
 254      ],
 255      200 => [
 256          'name' => 'Marina',
 257          'nickname' => 'estacet',
 258      ],
 259  ];
 260  
 261  $result = ArrayToXml::convert(['__numeric' => $array]);
 262  ```
 263  
 264  This will result in:
 265  
 266  ```xml
 267  <?xml version="1.0" encoding="UTF-8"?>
 268  <root>
 269      <numeric_100>
 270          <name>Vladimir</name>
 271          <nickname>greeflas</nickname>
 272      </numeric_100>
 273      <numeric_200>
 274          <name>Marina</name>
 275          <nickname>estacet</nickname>
 276      </numeric_200>
 277  </root>
 278  ```
 279  
 280  You can change key prefix with setter method called `setNumericTagNamePrefix()`.
 281  
 282  ### Using custom keys
 283  
 284  The package can also can handle custom keys:
 285  
 286  ```php
 287  $array = [
 288      '__custom:custom-key:1' => [
 289          'name' => 'Vladimir',
 290          'nickname' => 'greeflas',
 291      ],
 292      '__custom:custom-key:2' => [
 293          'name' => 'Marina',
 294          'nickname' => 'estacet',
 295          'tags' => [
 296              '__custom:tag:1' => 'first-tag',
 297              '__custom:tag:2' => 'second-tag',
 298          ]
 299      ],
 300  ];
 301  
 302  $result = ArrayToXml::convert($array);
 303  ```
 304  
 305  This will result in:
 306  
 307  ```xml
 308  <?xml version="1.0" encoding="UTF-8"?>
 309  <root>
 310      <custom-key>
 311          <name>Vladimir</name>
 312          <nickname>greeflas</nickname>
 313      </custom-key>
 314      <custom-key>
 315          <name>Marina</name>
 316          <nickname>estacet</nickname>
 317          <tags>
 318              <tag>first-tag</tag>
 319              <tag>second-tag</tag>
 320          </tags>
 321      </custom-key>
 322  </root>
 323  ```
 324  
 325  A custom key contains three, colon-separated parts: "__custom:[custom-tag]:[unique-string]".
 326  
 327  - "__custom"
 328    - The key always starts with "__custom".
 329  - [custom-tag]
 330    - The string to be rendered as the XML tag.
 331  - [unique-string]
 332    - A unique string that avoids overwriting of duplicate keys in PHP arrays.
 333  
 334  a colon character can be included within the custom-tag portion by escaping it with a backslash:
 335  
 336  ```php
 337  $array = [
 338      '__custom:ns\\:custom-key:1' => [
 339          'name' => 'Vladimir',
 340          'nickname' => 'greeflas',
 341      ],
 342      '__custom:ns\\:custom-key:2' => [
 343          'name' => 'Marina',
 344          'nickname' => 'estacet',
 345          'tags' => [
 346              '__custom:ns\\:tag:1' => 'first-tag',
 347              '__custom:ns\\:tag:2' => 'second-tag',
 348          ]
 349      ],
 350  ];
 351  ```
 352  This will result in:
 353  
 354  ```xml
 355  <?xml version="1.0" encoding="UTF-8"?>
 356  <root>
 357      <ns:custom-key>
 358          <name>Vladimir</name>
 359          <nickname>greeflas</nickname>
 360      </ns:custom-key>
 361      <ns:custom-key>
 362          <name>Marina</name>
 363          <nickname>estacet</nickname>
 364          <tags>
 365              <ns:tag>first-tag</ns:tag>
 366              <ns:tag>second-tag</ns:tag>
 367          </tags>
 368      </ns:custom-key>
 369  </root>
 370  ```
 371  
 372  ### Setting DOMDocument properties
 373  
 374  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.
 375  
 376  You can use the constructor to set DOMDocument properties.
 377  
 378  ```php
 379  $result = ArrayToXml::convert(
 380     $array, 
 381     $rootElement, 
 382     $replaceSpacesByUnderScoresInKeyNames, 
 383     $xmlEncoding, 
 384     $xmlVersion, 
 385     ['formatOutput' => true]
 386  );
 387  
 388  ```
 389  
 390  Alternatively you can use  `setDomProperties`
 391  
 392  ```php
 393  $arrayToXml = new ArrayToXml($array);
 394  $arrayToXml->setDomProperties(['formatOutput' => true]);
 395  $result = $arrayToXml->toXml();
 396  ```
 397  
 398  ### XML Prettification 
 399  
 400  Call `$arrayToXml->prettify()` method on ArrayToXml to set XML in pretty form.
 401  
 402  Example:
 403  
 404  ```php
 405  $array = [
 406      'Good guy' => [
 407          'name' => 'Luke Skywalker',
 408          'weapon' => 'Lightsaber'
 409      ],
 410      'Bad guy' => [
 411          'name' => 'Sauron',
 412          'weapon' => 'Evil Eye'
 413      ]
 414  ];
 415  $arrayToXml = new ArrayToXml($array);
 416  ```
 417  
 418  With prettification:
 419  
 420  ```php
 421  $arrayToXml->prettify()->toXml();
 422  ```
 423  
 424  will result in:
 425  
 426  ```xml
 427  <?xml version="1.0"?>
 428  <root>
 429      <Good_guy>
 430          <name>Luke Skywalker</name>
 431          <weapon>Lightsaber</weapon>
 432      </Good_guy>
 433      <Bad_guy>
 434          <name>Sauron</name>
 435          <weapon>Evil Eye</weapon>
 436      </Bad_guy>
 437  </root>
 438  ```
 439  
 440  Without prettification:
 441  
 442  ```php
 443  $arrayToXml->toXml();
 444  ```
 445  
 446  will result in:
 447  
 448  ```xml
 449  <?xml version="1.0"?>
 450  <root><Good_guy><name>Luke Skywalker</name><weapon>Lightsaber</weapon></Good_guy><Bad_guy><name>Sauron</name><weapon>Evil Eye</weapon></Bad_guy></root>
 451  ```
 452  
 453  ### Dropping XML declaration
 454  
 455  Call `$arrayToXml->dropXmlDeclaration()` method on ArrayToXml object to omit default XML declaration on top of the generated XML.
 456  
 457  Example:
 458  
 459  ```php
 460  $root = [
 461      'rootElementName' => 'soap:Envelope',
 462      '_attributes' => [
 463          'xmlns:soap' => 'http://www.w3.org/2003/05/soap-envelope/',
 464      ],
 465  ];
 466  $array = [
 467      'soap:Header' => [],
 468      'soap:Body' => [
 469          'soap:key' => 'soap:value',
 470      ],
 471  ];
 472  $arrayToXml = new ArrayToXml($array, $root);
 473  
 474  $result = $arrayToXml->dropXmlDeclaration()->toXml();
 475  ```
 476  
 477  This will result in:
 478  
 479  ```xml
 480  <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope/"><soap:Header/><soap:Body><soap:key>soap:value</soap:key></soap:Body></soap:Envelope>
 481  ```
 482  
 483  ### Adding processing instructions
 484  
 485  Call `$arrayToXml->addProcessingInstruction($target, $data)` method on ArrayToXml object to prepend a processing instruction before the root element.
 486  
 487  Example:
 488  
 489  ```php
 490  $arrayToXml = new ArrayToXml($array);
 491  $arrayToXml->addProcessingInstruction('xml-stylesheet', 'type="text/xsl" href="base.xsl"');
 492  $result = $arrayToXml->toXml();
 493  ```
 494  
 495  This will result in:
 496  
 497  ```xml
 498  <?xml version="1.0"?>
 499  <?xml-stylesheet type="text/xsl" href="base.xsl"?>
 500  <root><Good_guy><name>Luke Skywalker</name><weapon>Lightsaber</weapon></Good_guy><Bad_guy><name>Sauron</name><weapon>Evil Eye</weapon></Bad_guy></root>
 501  ```
 502  
 503  ## Testing
 504  
 505  ```bash
 506  vendor/bin/phpunit
 507  ```
 508  
 509  ## Changelog
 510  
 511  Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
 512  
 513  ## Contributing
 514  
 515  Please see [CONTRIBUTING](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for details.
 516  
 517  ## Security Vulnerabilities
 518  
 519  Please review [our security policy](../../security/policy) on how to report security vulnerabilities.
 520  
 521  ## Postcardware
 522  
 523  You're free to use this package, but if it makes it to your production environment we highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using.
 524  
 525  Our address is: Spatie, Kruikstraat 22, 2018 Antwerp, Belgium.
 526  
 527  We publish all received postcards [on our company website](https://spatie.be/en/opensource/postcards).
 528  
 529  ## Credits
 530  
 531  - [Freek Van der Herten](https://github.com/freekmurze)
 532  - [All Contributors](../../contributors)
 533  
 534  ## License
 535  
 536  The MIT License (MIT). Please see [License File](LICENSE.md) for more information.


Generated: Tue Jan 21 05:10:11 2025 Cross-referenced by PHPXref 0.7.1