Note that "if (! $sxe) {" may give you a false-negative if the XML document was empty (e.g. "<root />"). In that case, $sxe will be:
object(SimpleXMLElement)#1 (0) {
}
which will evaluate to false, even though nothing technically went wrong.
Consider instead: "if ($sxe === false) {"
XML hatalarıya ilgilenmek
Belgeleri yüklerken XML hatalarını ele almak basit bir işlemdir. Belgeyi yüklerken XML hatalarını libxml işlevselliğini kullanarak bastırdıktan sonra hatalar tek tek ele alınabilir.
libxml_get_errors() işlevi tarafından döndürülen libXMLError nesnesi, hata ile ilgili ileti, satır ve sütun gibi özellikler içerir.
Örnek 1 - Hatalı bir XML dizgesinin yüklenmesi
<?php
libxml_use_internal_errors(true);
$sxe = simplexml_load_string("<?xml version='1.0'><broken><xml></broken>");
if (!$sxe) {
echo "XML yüklemesi başarısız oldu\n";
foreach(libxml_get_errors() as $error) {
echo "\t", $error->message;
}
}
Yukarıdaki örneğin çıktısı:
XML yüklemesi başarısız oldu Blank needed here parsing XML declaration: '?>' expected Opening and ending tag mismatch: xml line 1 and broken Premature end of data in tag broken line 1
openbip at gmail dot com
25-Feb-2010 01:20
Jacob Tabak
15-Feb-2010 06:17
If you are trying to load an XML string with some escaped and some unescaped ampersands, you can pre-parse the string to ecsape the unescaped ampersands without modifying the already escaped ones:
<?php
$s = preg_replace('/&[^; ]{0,6}.?/e', "((substr('\\0',-1) == ';') ? '\\0' : '&'.substr('\\0',1))", $s);
?>
