PHP code example of subjective-php / dom

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

    

subjective-php / dom example snippets



use SubjectivePHP\Util;

$xml = <<<XML
<?xml version="1.0"


use SubjectivePHP\Util;

$catalog = [
    'book' => [
        [
            '@id' => '58339e95d52d9',
            'author' => 'Corets, Eva',
            'title' => 'The Sundered Grail',
            'genre' => 'Fantasy',
            'price' => 5.95,
            'published' => 1000094400,
            'description' => "The two daughters of Maeve, half-sisters, battle one another for control of England. Sequel to Oberon's Legacy.",
        ],
        [
            '@id' => '58339e95d530e',
            'author' => 'Randall, Cynthia',
            'title' => 'Lover Birds',
            'genre' => 'Romance',
            'price' => 4.95,
            'published' => 967867200,
            'description' => 'When Carla meets Paul at an ornithology conference, tempers fly as feathers get ruffled.',
        ],
    ],
];

$document = Util\DOMDocument::fromArray(['catalog' => $catalog]);
$document->formatOutput = true;
echo $document->saveXml();


use SubjectivePHP\Util;

$document = new DOMDocument();
$document->formatOutput = true;
Util\DOMDocument::addXPath($document, "/catalog/book[@id='58339e95d530e']/title", 'Lover Birds');
Util\DOMDocument::addXPath($document, '/catalog/book[@id="58339e95d530e"]/price', 4.95);
Util\DOMDocument::addXPath($document, '/catalog/book[@id="58339e95d52d9"]/title', 'The Sundered Grail');
Util\DOMDocument::addXPath($document, '/catalog/book[@id="58339e95d52d9"]/genre', 'Fantasy');
echo $document->saveXml();

array (
  'catalog' =>
  array (
    'book' =>
    array (
      0 =>
      array (
        '@id' => 'bk101',
        'author' => 'Gambardella, Matthew',
        'title' => 'XML Developer\'s Guide',
        'genre' => 'Computer',
        'price' => '44.95',
        'publish_date' => '2000-10-01',
        'description' => 'An in-depth look at creating applications with XML.',
      ),
      1 =>
      array (
        '@id' => 'bk102',
        'author' => 'Ralls, Kim',
        'title' => 'Midnight Rain',
        'genre' => 'Fantasy',
        'price' => '5.95',
        'publish_date' => '2000-12-16',
        'description' => 'A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world.',
      ),
    ),
  ),
)