When validating documents with this method there are two issues I don't like about it. First, it creates a bunch of warnings, which one would not expect, as the plot of calling this method is preventing any warnings that could occur when erroneously relying on the document's validity. Second, it only returns a boolean with no chance of getting additional details about the reasons for rendering invalid.
That's the reason for me to use a little wrapper, which I post here in case anyone finds it useful.
Please note that it only works with PHP5 or later.
<?php
class MyDOMDocument {
private $_delegate;
private $_validationErrors;
public function __construct (DOMDocument $pDocument) {
$this->_delegate = $pDocument;
$this->_validationErrors = array();
}
public function __call ($pMethodName, $pArgs) {
if ($pMethodName == "validate") {
$eh = set_error_handler(array($this, "onValidateError"));
$rv = $this->_delegate->validate();
if ($eh) {
set_error_handler($eh);
}
return $rv;
}
else {
return call_user_func_array(array($this->_delegate, $pMethodName), $pArgs);
}
}
public function __get ($pMemberName) {
if ($pMemberName == "errors") {
return $this->_validationErrors;
}
else {
return $this->_delegate->$pMemberName;
}
}
public function __set ($pMemberName, $pValue) {
$this->_delegate->$pMemberName = $pValue;
}
public function onValidateError ($pNo, $pString, $pFile = null, $pLine = null, $pContext = null) {
$this->_validationErrors[] = preg_replace("/^.+: */", "", $pString);
}
}
?>
<?php
// $doc is a DOMDocument object
$myDoc = new MyDOMDocument($doc); // copy constructor
// do anything with $myDoc that you would with $doc
$isValid = $myDoc->validate(); // won't create warnings
if (!$isValid) {
print_r($myDoc->errors); // the array all warnings are collected in
}
?>
Maybe you need to change the the part
preg_replace("/^.+: */", "", $pString)
to something different depending on your system's error reporting settings (HTML or plain text), whatsoever
Best Regards,
Anja
DOMDocument::validate
(No version information available, might be only in CVS)
DOMDocument::validate — Valide un document en se basant sur sa DTD
Description
bool DOMDocument::validate
( void
)
Valide un document en se basant sur sa DTD.
Vous pouvez utiliser la propriété validateOnParse de la classe DOMDocument pour effectuer une validation DTD.
Valeurs de retour
Cette fonction retourne TRUE en cas de succès, FALSE en cas d'échec. Si le document n'a aucune DTD d'attachée, cette méthode retournera FALSE.
Exemples
Exemple #1 Exemple de validation DTD
<?php
$dom = new DOMDocument;
$dom->Load('book.xml');
if ($dom->validate()) {
echo "Ce document est valide !\n";
}
?>
Vous pouvez également valider votre fichier XML en le chargeant :
<?php
$dom = new DOMDocument;
$dom->validateOnParse = true;
$dom->Load('book.xml');
?>
DOMDocument::validate
a dot schaffhirt at sedna-soft dot de
17-Sep-2008 11:22
17-Sep-2008 11:22
darren at viamedia dot co dot za
05-Aug-2008 04:19
05-Aug-2008 04:19
If you are loading xml with the intention of validating it against an internal dtd and you have experienced issues with the validation it could be related to missing LIBXML constants.
I found this post by "aidan at php dot net" in root level dom docs and thought it might be more useful here:
As of PHP 5.1, libxml options may be set using constants rather than the use of proprietary DomDocument properties.
DomDocument->resolveExternals is equivilant to setting
LIBXML_DTDLOAD
LIBXML_DTDATTR
DomDocument->validateOnParse is equivilant to setting
LIBXML_DTDLOAD
LIBXML_DTDVALID
PHP 5.1 users are encouraged to use the new constants.
Example:
<?php
$dom = new DOMDocument;
// Resolve externals
$dom->load($file, LIBXML_DTDLOAD|LIBXML_DTDATTR);
// OR
// Validate against DTD
$dom->load($file, LIBXML_DTDLOAD|LIBXML_DTDVALID);
$dom->validate();
?>
shinkan at gmail dot com
09-Jul-2008 05:40
09-Jul-2008 05:40
It doesn't seem to support "Internal DTD Declaration" as described by http://www.w3schools.com/dtd/dtd_intro.asp .
