php - 如果写入XML失败

标签 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'); 

请帮忙 :-)

最佳答案

检查save是否返回false:

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

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

关于php - 如果写入XML失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12351704/

相关文章:

error-handling - 克洛尤尔 :pre report failing value when destructuring

xml - 以@开头的变量名导致错误: Invalid token @ found on line X at column Y

python - 如何通过 Python 的 ElementTree 生成 XML 文件,并在输出中写入注册的命名空间

c# - 使用 OledbConnection 的 Linq to XML 和 Excel 之间的速度差异?

spring-boot - Spring Cloud Stream 错误处理...错误

php - 本地主机上的mysql程序奇怪的错误

php - 检查数组中的所有值是否都是 int

javascript - 当前 Ajax 请求的最佳实践

php - Android Web 服务 - "poke"Web 服务

javascript - 使用 catch 是否需要 throw