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
XMLReader::isValid
(PHP 5 >= 5.1.0)
XMLReader::isValid — Indica si el documento analizado es válido
Descripción
bool XMLReader::isValid
( void
)
Devuelve un booleano indicando si el documento siendo analizado es actualmente válido.
Valores devueltos
Devuelve TRUE en caso de éxito o FALSE en caso de error.
Ejemplos
Ejemplo #1 Validando el XML
<?php
$xml = XMLReader::open('test.xml');
// Se debe usar
$xml->setParserProperty(XMLReader::VALIDATE, true);
var_dump($xml->isValid());
?>
Ver también
- XMLReader::setParserProperty() - Establecer las opciones del analizador
- XMLReader::setRelaxNGSchema() - Establece el nomb re del archivo o el URI para un esquema RelaxNG
- XMLReader::setRelaxNGSchemaSource() - Establece los datos contenidos en un esquema RelaxNG
- XMLReader::setSchema() - Valida el documento en contra del XSD
zubin at trattonuovo dot com ¶
3 years ago
remy dot damour at laposte dot net ¶
4 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
anzenews at volja dot net ¶
5 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).
patryk dot szczyglowski at gmail dot com ¶
5 years ago
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.
