PHP code example of niji / xml-parser-bundle

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

    

niji / xml-parser-bundle example snippets




namespace App\Entity;

use Niji\XmlParserBundle\XmlParsingTrait;

class EntityName
{
    
    use XmlParsingTrait;
    
    ...
}



use Niji\XmlParserBundle\Parsers\XmlXPathParser;

class YourClass
{
    /**
    * @var \Niji\XmlParserBundle\Parsers\XmlXPathParser
    */
    protected $parser;
        
    /**
     * Inject the parser use Symfony's dependency injection.
     *
     * @param \Niji\XmlParserBundle\Parsers\XmlXPathParser $parser
     */
    public function __construct(XmlXPathParser $parser) {
        $this->parser = $parser;
    }
    
    /**
     * Parsing method.
     * 
     * @param string $sourceUrl
     * @param string $mappingName
     */
    public function yourMethod(string $sourceUrl, string $mappingName) {
        $xmlStr = file_get_contents($sourceUrl);
    
        // $result is a destination class or an array of destination classes or an associative array.
        $result = $this->parser->parse($xmlStr, $mappingName);
    }
}




namespace App\XmlProcessors;

use Niji\XmlParserBundle\Processor\XmlParsingProcessorInterface;

class BooleanProcessor implements XmlParsingProcessorInterface 
{

    /**
     * Process the passed value
     *
     * @param mixed $value
     *   Source value.
     * @param array $config
     *   Processor configuration.
     *
     * @return mixed
     *   Processed value.
     */
    public function process($value, array $config = []) {
        $processedValue = null;
        
        // Do whatever you need to process the $value
        // e.g: $processedValue = (boolean)$value;
        
        return $processedValue;
    }
}