如果写入XML失败

时间:2012-09-10 12:26:30

标签: php xml error-handling

无论如何都要检查xml写入期间是否发生了写入错误?我使用xml写入数据,但有时由于服务器故障导致数据损坏/未完成?

我想确保写入xml文档,否则再做一次!

$doc = new DOMDocument("1.0", "ISO-8859-1"); 
$doc->formatOutput = true; 

$r = $doc->createElement( "library" ); 
$doc->appendChild( $r ); 

$b = $doc->createElement( "data" ); 

$time = $doc->createElement( "updatetime" ); 
$time->appendChild($doc->createTextNode( $timestamp )); 
$b->appendChild( $time ); 

$update = $doc->createElement( "update" ); 
$update->appendChild($doc->createTextNode(0)); 
$b->appendChild( $update ); 

$data = $doc->createElement("thestring"); 
$data->appendChild( 
$doc->createTextNode($string) 
); 
$b->appendChild( $data ); 

$r->appendChild( $b ); 

$doc->save('xml_users/'.$currplayer.'.xml'); 

请帮助: - )

1 个答案:

答案 0 :(得分:0)

检查save是否返回false:

将所有内容包装到一个返回DOMDocument对象的函数中,然后调用它

$doc = createUser($vars);
if(!$doc->save('xml_users/'.$currplayer.'.xml')){
    // run code again
    $doc = createUser($vars);
}