PHP code example of gelight / php-json2sml

1. Go to this page and download the library: Download gelight/php-json2sml library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

gelight / php-json2sml example snippets




ight\conversion\{JsonToSmlConverter};

$json = <<<JSON
{
  "firstName": "John",
  "lastName": "Smith",
  "isAlive": true,
  "age": 27,
  "address": {
    "streetAddress": "21 2nd Street",
    "city": "New York",
    "state": "NY",
    "postalCode": "10021-3100"
	},
  "phoneNumbers": [
    {
      "type": "home",
      "number": "212 555-1234"
    },
    {
      "type": "office",
      "number": "646 555-4567"
    }
  ],
  "children": [
    "Aaron"
  ],
  "spouse": true
}
JSON;

$converter = new JsonToSmlConverter();
$doc = $converter::convert(json_decode($json));
$root = $doc->getRoot();

// Returns the first value of attribute "lastName"
echo $root
    ->attribute("lastName")
    ->getValues()[0];

// Returns the first value of attribute "city" in element "address" 
echo $root
    ->element("address")
    ->attribute("city")
    ->getValues()[0];

// Returns the first value of attribute "number" of second element "phoneNumbers" 
echo $root
    ->element("phoneNumbers")
    ->elements()[1]
    ->attribute("number")
    ->getValues()[0];

// Returns the first value of attribute "children"
echo $root
    ->attribute("children")
    ->getValues()[0];

# PHP
$doc->toString();

$converter = new JsonToSmlConverter();

$smlDocument = $converter::convert(json_decode($json));