It's always nice to see just where in the XML the error is so I've enhanced the error message example above to this:
// Read and parse dynamic PHP/XML document.
ob_start();
require($_REQUEST['page'].'.php');
$xml = ob_get_contents();
ob_end_clean();
$xmldoc = domxml_open_mem("<?xml version='1.0' encoding='ISO-8859-1'?>".$xml, DOMXML_LOAD_PARSING, $error);
// Show error.
if(!$xmldoc) {
// Assign each line to array.
$arXML = explode("\n", $xml);
echo "<pre>";
foreach ($error as $errorline) {
echo $errorline['errormessage'].
"Node: ".$errorline['nodename']."\n".
"Line: ".$errorline['line']."\n".
"Column: ".$errorline['col']."\n";
// Locate actual line number.
$arErrorMessage = explode(' line ', $errorline['errormessage']);
$arErrorMessage = explode(' ', $arErrorMessage[1]);
$errorLineNumber = $arErrorMessage[0];
// Show +/- 10 lines around error.
$minErrorLineNumber = max(0, $errorLineNumber-10);
$maxErrorLineNumber = min(sizeof($arXML), $errorLineNumber+10);
echo "XML snip (linies: ".$minErrorLineNumber." to ".$maxErrorLineNumber."):\n";
for($n = $minErrorLineNumber; $n < $maxErrorLineNumber; $n++)
if($n == $errorLineNumber)
echo "<B>".htmlentities($arXML[$n])."</B>\n";
else
echo htmlentities($arXML[$n])."\n";
echo "\n\n";
}
echo "</pre>";
}
This will actually render +/- 10 lines around the actual error. Hope others will find this usefull aswell.