PHP code example of arturaskaukenas / dom

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

    

arturaskaukenas / dom example snippets




use \ArturasKaukenas\DOM;

$content = \file_get_contents("./data/books.xml");
$parser = new DOM\XML\Parser();
$xml = $parser->fullParse($content);

use \ArturasKaukenas\DOM\XML\Parser;

$content = \file_get_contents("./data/books.xml");
$parser = new Parser();

$full_result = $parser->fullParse($content);

$node = $full_result->getChild(0)->getChild(0);

\count($result->getElementsByTagName("publish_date"))
$full_result->getElementsByTagName("id")[1]->getTextContents();

$full_result->getElementById("bk103");

$full_result->getElementById("text_content_test")->setTextContents("2000-12-15");

//Basic
$node->hasAttributes();
$node->hasAttribute("testAttribute");
$node->getAttributeNames()[1];
$node->getAttribute("testattribute");
$node->setAttribute("test", "2");
$node->removeAttribute("testAttribute");

//Child elements
$result = $full_result->getChild(0);
$result->currentChild()->getAttribute("id"); //Child 0
$result->nextChild()->getAttribute("id"); //Child 1
$result->nextChild()->getAttribute("id"); //Child 2

$result->resetChild();

$result->iterateChild()->getAttribute("id"); //Child 0
$result->iterateChild()->getAttribute("id"); //Child 1
$result->iterateChild()->getAttribute("id"); //Child 2

$result->endChild()->getAttribute("id");

//Append child
$test_node = new XML\StdNode;
$test_node->setName("TEST_NODE_1");
$test_node->setAttributes(array("id" => "test_node_id_1"));
$result->appendChild($test_node);

//Remove child
$result->removeChild($test_node);


use \ArturasKaukenas\DOM;

$parser = new DOM\XML\Parser();
$parser
    ->onFinalizeNode(
		"BOOK",
		function(DOM\INode $node) {
			/*
				$node->AUTHOR ..
				$node->TITLE ..
			*/
		}
	);
	$parser->prepare();

	$handle = \fopen($folder."books.xml", "r");
	while (!\feof($handle)) {
		$buffer = \fgets($handle, 4096);
		$parser->getCallBack()($buffer);
	}
	\fclose($handle);

use \ArturasKaukenas\DOM;

class BOOK extends DOM\XML\Node {
	public function __construct() {
		$this->
			expects(
				(new DOM\Expected("author", DOM\NodeDataTypes::T_STRING))->
					process(
						function ($value) : string {
							return \strtoupper((string) $value);
						}
					)
			)->
			setExpectedValue("title", DOM\NodeDataTypes::T_STRING)->
			setExpectedValue("genre", DOM\NodeDataTypes::T_STRING)->
			expects(
				(new DOM\Expected("price", DOM\NodeDataTypes::T_FLOAT))->
					validate(//Example
						function (float $value) {
							if ($value > 10) {
								return "Should be cheaper than 10";
							}

							return true;
						}
					)
			)->
			setExpectedValue(
				"publish_date",
				DOM\NodeDataTypes::T_MIXED,
				function($value) {
					$timestamp = \strtotime((string) $value);
					if ($timestamp === false) {
						return null;
					}

					return (new \DateTime())->setTimestamp($timestamp);
				}
			)->
			setExpectedValue("description", DOM\NodeDataTypes::T_STRING);
	}
}

$parser = new DOM\XML\Parser();
$parser
    ->onFinalizeNode(
		"BOOK",
		function(DOM\INode $node) {
			echo "Title: ".$node->TITLE."\n";
			echo "Author: ".$node->AUTHOR."\n";
			echo "Price: ".$node->PRICE."\n";
			echo "Date: ".$node->PUBLISH_DATE->format(DateTime::ATOM)."\n";
		}
	);
	$parser->prepare();

	$handle = \fopen($folder."books.xml", "r");
	while (!\feof($handle)) {
		$buffer = \fgets($handle, 4096);
		$parser->getCallBack()($buffer);
	}
	\fclose($handle);

public function expects(Expected $expected): INode;

public function setExpectedObject(string $name, $object, bool $isArray): INode;

public function setExpectedValue(string $name, int $type, ?callable $processFunction = null): INode;

public function useDataParser(callable $callback): void;

public function setIgnoreChildren(bool $value): void;

public function setIgnoreChildrenIfNotExists(bool $value): void;

public function setDataAsChildren(bool $value): void;

public function expectedElementExists(string $name) : bool;

public function setExpectedElement(string $name, $value) : void;

public function __get(string $name) : mixed;

public function __set(string $name, $value) : void;

public function __isset(string $name) : bool;

public function hasAttributes(): bool;

public function hasAttribute(string $name): bool;

public function getAttributeNames(): array;

public function getAttribute(string $name): ?string;

public function setAttribute(string $key, $value): void;

public function removeAttribute(string $name): void;

public function getChild(int $num): ?INode;

public function currentChild(): ?INode;

public function nextChild(): ?INode;

public function iterateChild(): ?INode;

public function endChild(): ?INode;

public function resetChild(): ?INode;

public function getElementsByTagName(string $tagName, bool $clean = true): array;

public function getElementById(string $id): ?INode;

public function getTextContents(): string;

public function setTextContents(?string $data): void;

public function getInner(): string;

public function setInner(?string $data): void;

public function setName(string $name, bool $clean) : void;

public function setAttributes(array $attributes) : void;

public function appendChild(INode $child) : INode;

public function removeChild(INode $childNode) : INode;

public function remove() : void;

public function cleanChildren() : void;

public function setParser(IParser $parser) : void;

public function getExpected() : array;

public function getInner() : string;

public function getInnerXML() : string;

public function setInner(?string $data) : void;

public function setInnerXML(?string $data) : void;

public function init(?INode $currentScope = null, bool $skipParser = false) : void;

public function parse(string $data) : bool;

public function finalize() : void;

public function errorsExists() : bool;

public function getErrors() : array;

public function clean() : void;

public function registerNode(\ReflectionClass $class) : IParser;

public function isNodeRegistered(string $elementName) : bool;

public function onFinalizeNode(string $name, callable $callback) : IParser;

public function __construct(string $name, int $type = NodeDataTypes::T_MIXED);

public function typeOf(int $type);

public function preValidate(callable $function) : Expected;

public function process(callable $function) : Expected;

public function validate(callable $function) : Expected;

public function prototypeOfClassName(string $name) : Expected;

public function prototypeOfObject(\ReflectionClass $class) : Expected;

public static function getTypeName(int $type);