2012-07-27 119 views
1

我想根據XSD文件驗證XML並在出現錯誤時獲取錯誤。驗證XML與XSD的錯誤

當我在DOM中使用它而不是XMLReader時,它工作正常。據瞭解,他們都使用libxml庫,所以我試圖使用它的XMLReader,但沒有運氣。

感謝

DOM(正常工作)

libxml_use_internal_errors(true); 

if (! $xml->schemaValidate($xsd_file)) 
{ 
    get_errors(); 
    exit; 
} 

function get_errors() 
{ 
    $messages = null; 
    $errors  = libxml_get_errors(); 

    foreach ($errors as $error) 
    { 
     switch ($error->level) 
     { 
      case LIBXML_ERR_ERROR: 
       $messages .= 'Error ' . $error->code . $error->message; 
       break; 

      case LIBXML_ERR_WARNING: 
       $messages .= 'Warning ' . $error->code . $error->message; 
       break; 

      case LIBXML_ERR_FATAL: 
       $messages .= 'Fatal ' . $error->code . $error->message; 
       break; 
     } 

     echo $messages .= ($error->file) ? $error->file : $error->line; 
    } 
} 

的XmlReader

libxml_use_internal_errors(true); 

//This returns true all times whether XML has faults or not 
if (! $reader->setSchema($xsd_file)) 
{ 
    //This echos nothing whether XML has faults or not 
    get_errors(); 
} 

回答

0

在設置模式(不能與上面相同的誤差函數工作) ,我沒有看到任何read()或isValid()繼續,所以你會如何得到錯誤?看看documentation(我假設PHP),它應該可以幫助你瞭解一個想法。

+0

問題是我在XML,XSD,解析器等方面都很新,所以雖然我閱讀所有這些文檔,但對於我來說這是有點困難的。我試圖把isValid()其他函數放在一起,但沒有任何改變。 Google也一樣。我想我需要更多經驗或幫助。 – BentCoder 2012-07-27 19:43:52