PHP code example of jolti / dtotoxml

1. Go to this page and download the library: Download jolti/dtotoxml 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/ */

    

jolti / dtotoxml example snippets


use Dtotoxml\XmlSerializer;
use Dtotoxml\Configuration;
use Samples\Dto\School;
use Samples\Dto\SchoolFixtures;

$xmlSerialize = new XmlSerializer();

// Configure the serializer
$config = new Configuration();
$config->setHead('<?xml version="1.0"

$school = SchoolFixtures::createSchool();
$xml = $xmlSerialize->format($xmlSerialize->serialise($school));

// Output the XML
echo '<pre>', htmlentities($xml), '</pre>';

$xmlString = SchoolFixtures::createSchoolXml();
$object = $xmlSerialize->unserialise($xmlString, School::class);

// Access the deserialized object
var_dump($object);

namespace Samples\Dto;

class School
{
    private $name;
    private $adresse;
    private $teachers;
    private $rooms;

    public function getAttributes(): array
    {
        return ["name"];
    }

    public function getName(): string
    {
        return $this->name;
    }

    public function setName(string $name): void
    {
        $this->name = $name;
    }

    public function getAdresse(): Adresse
    {
        return $this->adresse;
    }

    public function setAdresse(Adresse $adresse): void
    {
        $this->adresse = $adresse;
    }

    public function getTeachers(): array
    {
        return $this->teachers;
    }

    public function setTeachers(array $teachers): void
    {
        $this->teachers = $teachers;
    }

    public function getRooms(): array
    {
        return $this->rooms;
    }

    public function setRooms(array $rooms): void
    {
        $this->rooms = $rooms;
    }
}

  /**
   * @isAttribute
   * @var string $id
   */
  private string $id;
  

  /**
   * @outputName adresse-city
   * @inputName adresse-city
   * @var string $city
   */
  private $city;
  

  /**
   * @outputName adresse-city
   * @inputName adresse-city
   * @var string $city
   */
  private $city;
  
xml
      <Adresse>
          <adresse-city>Tangier</adresse-city>
      </Adresse>
      
xml
      <Adresse>
          <adresse-city>Tangier</adresse-city>
      </Adresse>