PHP code example of byjg / anydataset-xml

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

    

byjg / anydataset-xml example snippets



$xml = file_get_contents('example1.xml');

$dataset = new \ByJG\AnyDataset\Xml\XmlDataset(
    $xml,        // The Xml
    "book",       // The node that represents a row
    [
        "category" => "@category",
        "title" => "title",
        "lang" => "title/@lang",
        "lang2" => function ($row) {
            return substr($row->get('lang'), 0, 2);
        }
    ] // Mapping columns
);

$iterator = $dataset->getIterator();
foreach ($iterator as $row) {
    echo $row->get('category'); // Print COOKING, CHILDREN, WEB
    echo $row->get('title');    // Print Everyday Italian, Harry Potter, Learning Xml
    echo $row->get('lang');     // Print en-US, de-DE, pt-BR
    echo $row->get('lang2');    // Print en, de, pt
}


$xml = file_get_contents('example2.xml')

$namespace = array(
    "fake" => "http://www.w3.org/2005/Atom",
    "gd" => "http://schemas.google.com/g/2005"
);

$rootNode = 'fake:entry';
$colNode = array("id" => "fake:id", "updated" => "fake:updated", "name" => "fake:title", "email" => "gd:email/@address");
$xmlDataset = new \ByJG\AnyDataset\Xml\XmlDataset(
    $xml,
    $rootNode,
    $colNode,
    $namespace
);
$xmlIterator = $xmlDataset->getIterator();