PHP 8.4.1 Released!

XMLReader::isValid

(PHP 5 >= 5.1.0, PHP 7, PHP 8)

XMLReader::isValidパースしているドキュメントの妥当性を示す

説明

public XMLReader::isValid(): bool

DTD や RelaxNG schema を参照し、 パースしているドキュメントが妥当なものであるかどうかを論理型で返します。 schema がない場合、かつ DTD の検証オプションが指定されない場合、 このメソッドは false を返します。

パラメータ

この関数にはパラメータはありません。

戻り値

ドキュメントが妥当である場合は true を返します。 そうでない場合、false を返します。

例1 XML の妥当性の検証

<?php
$xml
= XMLReader::open('test.xml');

// validate オプションを有効にしておかないと、
// このメソッドは正しく動作しません
$xml->setParserProperty(XMLReader::VALIDATE, true);

var_dump($xml->isValid());
?>

注意

注意: このメソッドは、現在のノードだけをチェックします。ドキュメント全体をチェックするわけではありません。

参考

add a note

User Contributed Notes 4 notes

up
3
remy dot damour at laposte dot net
15 years ago
1. If you validate against relax-ng, no need to call $xml->setParserProperty(XMLReader::VALIDATE, true);

2. Be aware that $xml->isValid() will return validity for currently active node (ie. node currently positioned using $xml->read()). It won't check validity of your entire tree at once, but rather on a step by step basis
up
2
me at lubu dot ch
2 years ago
Be aware that $xml->isValid() will return validity only for currently active node, so you have to loop trough the nodes.

Here is a example how to validate a entire XML file against a XSD schema:

<?php

$xmlReader
= new \XMLReader();
$xmlReader->open('./example.xml');
$xmlReader->setParserProperty(\XMLReader::VALIDATE, true);
$xmlReader->setSchema('./schema.xsd');

\libxml_use_internal_errors(true);

$msgs = [];

while (
$xmlReader->read()) {
if (!
$xmlReader->isValid()) {
$err = \libxml_get_last_error();
if (
$err && $err instanceof \libXMLError) {
$msgs[] = \trim($err->message) . ' on line ' . $err->line;
}
}
}

if (
$msgs) {
throw new
\Exception("XML schema validation errors:\n - " . implode("\n - ", array_unique($msgs)));
}
?>
up
0
zubin at trattonuovo dot com
14 years ago
I encountered some problems to use isValid method with xml2assoc function.
I use this way to validate the entire xml file and put it into an associative array.

$xml = new XMLReader();
if (!$xml->xml($xml_string, NULL, LIBXML_DTDVALID)) {
echo "XML not valid: load error";
exit();
}

libxml_use_internal_errors(TRUE);

$xml_array = xml2assoc($xml);

$arErrors = libxml_get_errors();
$xml_errors = "";
foreach ($arErrors AS $xmlError) $xml_errors .= $xmlError->message;
if ($xml_errors != "") {
echo "XML not valid: ".$xml_errors;
exit();
}

//all ok
up
0
anzenews at volja dot net
16 years ago
This comment is only partially correct:
"isValid() always returns false unless you enable checking for validity by $reader->setParserProperty(XMLReader::VALIDATE, true);"
This enables DTD checking, but you can also check by using RelaxNG (see setRelaxNGSchema() and setRelaxNGSchemaSource()).

And also, this is NOT correct:
"If you just need to check if XML file is well formed, successful loading into XMLReader object is usually enough."
It is not enough. Pull parsers operate on stream and if you have a large enough file they will not know it is well formed until it is read to the end. If you need to know if it is well formed or/and valid, read it till the end or validation error (you can use next() for fast reading if you don't care about contents).
To Top