PHP code example of jgswift / xral

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

    

jgswift / xral example snippets


$query = new XML\Simple();

$query->select('//book')
      ->from('library.xml')
      ->where('authors/author','Stephen King');

$result = $query();

var_dump($result); // qinq\Collection [ SimpleXMLElement, SimpleXMLElement, ... ]

$file = new qio\File('library.xml');

$query = new XML\Simple();

// set the author of a specific book
$query->select('library/books/book')
      ->update($file)
      ->set('author','Stephen King')
      ->where('name','The Langoliers');

$query();

$file = new qio\File('library.xml');

$query = new XML\Simple();
            
$query->select('/library/books')
      ->update($file)
      ->insert(['book' => [
          'name' => 'The Catcher In The Rye',
          'authors' => [
              'author' => 'J. D. Salinger'
          ],
          'ISBN' => '0316769533 9780316769532',
          'publisher' => 'Amazon',
          'pages' => 277
      ]]);

$query();

$file = new qio\File('library.xml');

$query = new XML\Simple();

$query->delete('//book')
      ->update($file)
      ->where('name','The Catcher In The Rye');

$query();

$query = new XML\DOM();

$query->select('//book')
      ->from('library.xml')
      ->where('authors/author','Stephen King');

$result = $query();

var_dump($result); // qinq\Collection [ DOMElement, DOMElement, ... ]

$file = new qio\File('library.xml');

$query = new XML\DOM();

$query->select('library/books/book')
      ->update($file)
      ->set('author','Stephen King')
      ->where('name','The Langoliers');

$query();

$file = new qio\File('library.xml');

$query = new XML\DOM();
            
$query->select('/library/books')
      ->update($file)
      ->insert(['book' => [
          'name' => 'The Catcher In The Rye',
          'authors' => [
              'author' => 'J. D. Salinger'
          ],
          'ISBN' => '0316769533 9780316769532',
          'publisher' => 'Amazon',
          'pages' => 277
      ]]);

$query();

$file = new qio\File('library.xml');

$query->delete('//book')
      ->update($file)
      ->where('name','The Catcher In The Rye');

$query();

$query = new INI\Query();
            
$query->section('general')
      ->from('config.ini')
      ->where('debug',0);

$result = $query();

var_dump($result); // qinq\Collection [ array, array, ... ]

$file = new qio\File('config.ini');

$query = new INI\Query();

// update debug setting in general section to 1
$query->update($file)
      ->section('general')
      ->set('debug',1);

$query();

$file = new qio\File('config.ini');

$query->update($file)
      ->section('general')
      ->insert('name','My Application');

$query();

$file = new qio\File('config.ini');

$query = new INI\Query();
            
$query->update($file)
      ->section('general')
      ->delete('name');

$query();

$query = new YML\Query();

$query->select('product')
      ->from('invoice.yml')
      ->where('quantity',1);

$result = $query();

var_dump($result); // qinq\Collection [ array, array, ... ]

$query = new YML\Query();

$query->select('product')
      ->update('invoice.yml')
      ->where('sku','BB5280R')
      ->set('quantity',5);

$query();

$query = new YML\Query();
            
$query->select('product')
      ->update('invoice.yml')
      ->insert([
          'sku' => 'BB5280R',
          'quantity' => 6,
          'description' => 'Baseball Glove',
          'price' => 50
      ]);

$query();

$query = new YML\Query();

$query->delete('product')
      ->update('invoice.yml')
      ->where('sku','BB5280R');

$query();

$query = new JSON\Query();

$query->select(function($person) {
            return $person['firstName'].' '.$person['lastName']
      })
      ->from('people.json')
      ->where(function($person) {
            return ($person['money'] > 5000) ? true : false;
      });

$result = $query();

var_dump($result); // qinq\Collection [ [ 'name' => 'john doe' ], [ 'name' => 'billy bob' ] ]

$file = new qio\File('people.json');

$query = new JSON\Query();

// change a persons firstname from 'billy' to 'bob'
$query->update($file)
      ->set('firstName','bob')
      ->where(function($person) {
          return ($person['firstName'] == 'billy') ? true : false;
      });

$query();

$file = new qio\File('people.json');

$query = new JSON\Query();
            
$query->update($file)
      ->insert([
          'firstName' => 'jane',
          'lastName' => 'doe',
          'gender' => 'female',
          'money' => 50000
      ]);

$query();

$file = new qio\File('people.json');

$query = new JSON\Query();
            
$query->update($file)
      ->delete()
      ->where('gender','female');

$query();
sh
php composer.phar