PHP code example of hyperized / xml-validator

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

    

hyperized / xml-validator example snippets


use Hyperized\Xml\Exceptions\InvalidXml;
use Hyperized\Xml\Exceptions\XmlValidatorException;
use Hyperized\Xml\Validator;

$validator = new Validator();

try {
    $validator->validateXMLFile('/path/to/document.xml', '/path/to/schema.xsd');
} catch (InvalidXml $exception) {
    // Malformed, or rejected by the schema. Line and column survive.
    foreach ($exception->getErrors() as $error) {
        printf("line %d column %d: %s\n", $error->line, $error->column, trim($error->message));
    }
} catch (XmlValidatorException $exception) {
    // Missing, unreadable or empty file.
    printf("%s: %s\n", $exception::class, $exception->getMessage());
}

$validator->validateXMLFile('/path/to/document.xml');
$validator->validateXMLString($xml);
$validator->validateXMLString($xml, '/path/to/schema.xsd');

$validator->isXMLFileValid('/path/to/document.xml');
$validator->isXMLFileValid('/path/to/document.xml', '/path/to/schema.xsd');

$validator->isXMLStringValid($xml);
$validator->isXMLStringValid($xml, '/path/to/schema.xsd');

use Hyperized\Xml\ValidatorInterface;

public function __construct(private readonly ValidatorInterface $validator) {}

$validator = new Validator(version: '1.1', encoding: 'iso-8859-1');

$validator->getVersion();   // '1.1'
$validator->getEncoding();  // 'iso-8859-1'