DOMDocument->validate()

(no version information, might be only in CVS)

DOMDocument->validate() --  Validates the document based on its DTD

说明

class DOMDocument {

bool validate ( void )

}

Validates the document based on its DTD.

You can also use the validateOnParse property of DOMDocument to make a DTD validation.

返回值

如果成功则返回 TRUE,失败则返回 FALSE。 If the document have no DTD attached, this method will return FALSE.

范例

例子 1. Example of DTD validation

<?php
$dom
= new DOMDocument;
$dom->Load('book.xml');
if (
$dom->validate()) {
    echo
"This document is valid!\n";
}
?>

You can also validate your XML file while loading it:

<?php
$dom
= new DOMDocument;
$dom->validateOnParse = true;
$dom->Load('book.xml');
?>


add a note add a note User Contributed Notes
aidan at php dot net
05-Dec-2004 01:31
This method throws E_NOTICE errors for any validation errors. If you'd rather store validation errors in an array for gracious handling, you can use the code below.

<?php
/**
 * Hold thrown errors statically
 */
function staticerror($errno, $errstr, $errfile, $errline, $errcontext, $ret = false)
{
   static
$errs = array();

   if (
$ret === true) {
       return
$errs;
   }

  
$tag = 'DOMDocument::validate(): ';
  
$errs[] = str_replace($tag, '', $errstr);
}

// Load a document
$dom = new DOMDocument;
$dom->load('somefile');

// Set up error handling
set_error_handler('staticerror');
$old = ini_set('html_errors', false);

// Validate
$doc->validate();

// Restore error handling
ini_set('html_errors', $old);
restore_error_handler();

// Get errors
$errs = staticerror(null, null, null, null, null, true);
print_r($errs);

?>