PHP code example of jeidison / paxb

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

    

jeidison / paxb example snippets



...

class DateBrAdapter implements XmlAdapter
{
    public function marshal(mixed $object): mixed
    {
        return $object->format('d/m/Y');
    }

    public function unmarshal(mixed $object): mixed
    {
        return DateTime::createFromFormat('d/m/Y', $object)
                       ->setTime(null, null, null);
    }
}


...

#[XmlType(propOrder: ["number", "street", "fullAddress"])]
class Address
{
    #[XmlTransient]
    private string $fullAddress;

    #[XmlElement("street")]
    private string $address;

    #[XmlElement]
    private string $number;
    
.
.
.    

class Author
{
    #[XmlElement]
    private string $name;

    #[XmlElement]
    private ?string $birthday = null;

    #[XmlElement]
    private ?string $email = null;

    private ?Address $address = null;
    
.
.
.    

#[XmlRootElement("livros")]
class Book
{
    #[XmlAttribute("identificador")]
    private int $id;

    #[XmlElement("nome")]
    private String $name;

    #[XmlPhpTypeAdapter(DateBrAdapter::class)]
    private DateTime $data;

    /**@var array<Author> */
    #[XmlElement("authors")]
    private array $authors;
    
    ...


...

$address = new Address();
$address->setAddress("Rua 10");
$address->setNumber("123");
$address->setFullAddress("123");

$author = new Author();
$author->setName("Jeidison Farias");
$author->setBirthday("");
$author->setEmail("");
$author->setAddress($address);

$book = new Book();
$book->setId(1);
$book->setName("PHP XML Binding");
$book->setAuthors([$author]);
$book->setData(new DateTime());
$book->setAddress($address)

$paxb = PAXB::createMarshaller();
$xml  = $paxb->marshal($book);

echo $xml;
...


...

$xml = 'O seu XML';

$unmarshaller = PAXB::createUnmarshal(Book::class);
$book = $unmarshaller->unmarshal($xml);



...

$parameter = new Xsd2PhpParameter;
$parameter->pathRootXsd = '';
$parameter->withSetters = true;
$parameter->withGetters = true;
$parameter->namespace = '';
$parameter->pathStoreClasses = '';

Xsd2Php::instance()->convert($parameter);