PHP xml_error_string() 関数

定義と使用方法

xml_error_string() 関数は XML 解析器のエラーデスクリプトを取得します。

構文

xml_error_string(errorcode)
パラメータ 説明
errorcode 必要です。使用されるエラーコードを定義します。このエラーコードは xml_get_error_code() 関数の返値です。

説明

を返します errorcode 説明のエラーコードパラメータに対応するテキスト記述文字列を返します。対応する説明がない場合は false を返します。

<?php
// 無効な xml ファイル
$xmlfile = 'test.xml';
$xmlparser = xml_parser_create();
// ファイルを開きデータを読み取る
$fp = fopen($xmlfile, 'r');
while ($xmldata = fread($fp, 4096)) 
  {
  // データブロックの解析
  if (!xml_parse($xmlparser,$xmldata,feof($fp))) 
    {
    die( print "ERROR: "
    . xml_error_string(xml_get_error_code($xmlparser))
    . "<br />"
    . "Line: "
    . xml_get_current_line_number($xmlparser)
    . "<br />"
    . "Column: "
    . xml_get_current_column_number($xmlparser)
    . "<br />");
    }
  }
xml_parser_free($xmlparser);
?>

出力:

ERROR: マッチングタグ不一致
Line: 8
Column: 51