我有两个文件:
- 示例 XML 文件。
- 带有模式的 .xsd 文件,上述 XML 文件必须遵守该模式。
为了根据模式验证 XML 文件,我一直在使用:
$dom = new DOMDocument();
//$this->xmlstr; is my XML file after being loaded into a string.
$dom->loadXML($this->xmlstr);
//$xsd_file is definitely my xsd file.
if(!$dom->schemaValidate($xsd_file)){
$errors = libxml_get_errors(); //supposed to give back errors?
var_dump($errors); //debugging - shows: array empty
}
但是,只要我的 XML 文档不遵守架构中的规则,我就会不断收到警告错误。
Warning: DOMDocument::schemaValidate() [domdocument.schemavalidate]: Element 'Header': This element is not expected. Expected is ( Routing )
我一直在故意搞砸我的 XML 文件,只是为了看看 $dom->schemaValidate 实际上是如何处理它的。显然,我不希望 PHP 在 XML 不符合模式时将警告消息吐到页面上。相反,我希望我的应用程序能够解决这个问题。我在这里忽略了什么吗?
最佳答案
你必须打电话
libxml_use_internal_errors(true);
在创建新的 DOMDocument() 之前,为了抑制警告并开始将 XML 解析错误收集到可通过 libxml_get_errors() 访问的内部结构中。
关于php - DOMDocument::schemaValidate() 抛出警告错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12368453/