PHP code example of borsodigerii / php-xml-chunker

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

    

borsodigerii / php-xml-chunker example snippets


$chunkSize = 100;
$outputFilePrefix = "out-";
$xmlfile = "bigFile.xml";
$validationFunction = fn($data, $tag) => {
    return true;
}
$checkingTags = array();

$chunker = new Chunker\Chunker($xmlfile, $chunkSize, $outputFilePrefix, $validationFunction, $checkingTags);

// ... the instance is created in $chunker
$chunker.chunkXML("item", "root");

// ... 
$log = $chunker.chunkXML(....);
echo $log;

/*

[timestamp] Starting new chunking...
[timestamp] ..
[timestamp] ..
*/


$chunkSize = 1000; // Max. 1000 shopItems per chunk
$xmlfile = "feed.xml"; // Input file
$outPrefix = "feed-"; // Prefix for chunk-files
$checkingTags = array("weight_kg"); // Tags to be validated (to which the validation function will be called)

// validation function
function validation($data, $tag) {
    if($tag == "weight_kg"){
        if(!empty($data) && intval($data) > 10) return true;
    }
    return false;
}

// Tags to be counted with $chunkSize
$mainTag = "shopItem";

// Root tag/element, that is only present once in the xml file
$rootTag = "Shop";

// Creating the chunker instance, and running the Chunker
$chunker = new Chunker\Chunker($xmlfile, $chunkSize, $outPrefix, "validation", $chekingTags);
$chunker.chunkXML($mainTag, $rootTag);
bash
$ composer