JSON Builder - PHP Online

Form of PHP Sandbox

Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php in the places where the PHP script should be executed.



Your result can be seen below.

Result of php executing





Full code of JSON Builder.php

  1. <?php
  2.  
  3. $xml = new SimpleXMLElement('<xml/>');
  4.  
  5. for ($i = 1; $i <= 8; ++$i) {
  6.     $track = $xml->addChild('track');
  7.     $track->addChild('path', "song$i.mp3");
  8.     $track->addChild('title', "Track $i - Track Title");
  9. }
  10.  
  11. $xml_content = formatXml($xml);
  12. print( $xml_content );
  13.  
  14.  
  15.  
  16. function formatXml($simpleXMLElement)
  17. {
  18.     $xmlDocument = new DOMDocument('1.0');
  19.     $xmlDocument->preserveWhiteSpace = false;
  20.     $xmlDocument->formatOutput = true;
  21.     $xmlDocument->loadXML($simpleXMLElement->asXML());
  22.  
  23.     return $xmlDocument->saveXML();
  24. }
File Description
  • JSON Builder
  • PHP Code
  • 18 Oct-2019
  • 561 Bytes
You can Share it: