vrbata/xml-validator

Validates your XML against a xsd schema.

v2.0.1 2021-12-02 11:11 UTC

This package is not auto-updated.

Last update: 2024-05-02 22:30:34 UTC


README

Build Status Packagist

Validate Xml against a xsd schema.

Installation

Library

$ git clone https://github.com/vrbata/XmlValidator.git

Composer

$ composer require vrbata/xml-validator:dev-master

Usage

<?php
require "./vendor/autoload.php";
use XmlValidator\XmlValidator;

$xml = "<sample>my xml string</sample>";
$xsd = "path_to_xsd.xsd";

// Validate based on xsd file
$xmlValidator = new XmlValidator($xml, $xsd, XsdSource::FILE);
try{
    $xmlValidator->validate($xml,$xsd);
    
    // Check if is valid
    if(!$xmlValidator->isValid()){
        
        // Do whatever with the errors.
        foreach ($xmlValidator->errors as $error) {
            /*echo sprintf('[%s %s] %s (in %s - line %d, column %d)',
                $error->level, $error->code, $error->message, 
                $error->file, $error->line, $error->column
            ); */
        }
    }
} catch (\InvalidArgumentException $e){
    // catch InvalidArgumentException
}

// Validate based on xsd as string
$xmlValidator = new XmlValidator($xml, $xsd, XsdSource::STRING);
try{
    $xmlValidator->validate($xml,$xsd);
    
    // Check if is valid
    if(!$xmlValidator->isValid()){
        
        // Do whatever with the errors.
        foreach ($xmlValidator->errors as $error) {
            /*echo sprintf('[%s %s] %s (in %s - line %d, column %d)',
                $error->level, $error->code, $error->message, 
                $error->file, $error->line, $error->column
            ); */
        }
    }
} catch (\InvalidArgumentException $e){
    // catch InvalidArgumentException
}

Based on XmlUtils from Symfony config component.