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
XMLReader::isValid
(PHP 5 >= 5.1.2)
XMLReader::isValid — パースしているドキュメントの妥当性を示す
説明
bool XMLReader::isValid
( void
)
パースしているドキュメントが妥当なものであるかどうかを論理型で返します。
返り値
成功した場合に TRUE を、失敗した場合に FALSE を返します。
例
例1 XML の妥当性の検証
<?php
$xml = XMLReader::open('test.xml');
// これを使用しなければなりません
$xml->setParserProperty(XMLReader::VALIDATE, true);
var_dump($xml->isValid());
?>
参考
- XMLReader::setParserProperty - パーサのオプションを設定または設定解除する
- XMLReader::setRelaxNGSchema - RelaxNG スキーマのファイル名あるいは URI を設定する
- XMLReader::setRelaxNGSchemaSource - RelaxNG スキーマを含むデータを設定する
XMLReader::isValid
remy dot damour at laposte dot net
31-Dec-2008 08:27
31-Dec-2008 08:27
anzenews at volja dot net
23-Jan-2008 03:50
23-Jan-2008 03:50
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).
patryk dot szczyglowski at gmail dot com
21-Dec-2007 11:10
21-Dec-2007 11:10
isValid() always returns false unless you enable checking for validity by:
$reader->setParserProperty(XMLReader::VALIDATE, true);
where $reader is XMLReader object.
Beware, once you enable this check, PHP will search for DTD file and possibly produce warnings.
If you just need to check if XML file is well formed, successful loading into XMLReader object is usually enough.
