PHP code example of ttbooking / xml-mapper

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

    

ttbooking / xml-mapper example snippets


use XmlMapper\Elements\NodeArray;
use XmlMapper\Elements\NodeElement;

/**
* Class ChildNodeArray
 */
class ChildNodeArray extends NodeArray {
    protected string $className = TextItem::class;
}

/**
* Class TextItem
 */
class TextItem extends NodeElement {
    protected ?string $_name = 'item_text'; // setup node element name
}

/**
 * Class ChildNode
 * @property string $Value
 */
class ChildNode extends NodeElement {
//if name not set - use class name as is
}

class SimpleStruct extends NodeElement {
    protected ChildNode $childNode;
    protected ChildNodeArray $array;
    
    public function getChildNode() {
        return $this->childNode;
    }
    
    public function getArray() {
        return $this->array;
    }
    
}

$xml = 'xml from top'; //

///...
$root = SimpleStruct::mapFromXml($xml);

echo $root->getNamespace()->getPrefix(); //prefix
echo $root->getNamespace()->getUri(); //uri

echo $root->getChildNode()->Value;// magic properties == attributes 
echo $root->getChildNode(); //to string - get node text
foreach ($root->getArray() as $textItem) { //iterable
    echo $textItem; // "text number 1", than "text number 2"
}

echo $root->toXml();