Created
July 9, 2013 11:46
-
-
Save 7ochem/5956744 to your computer and use it in GitHub Desktop.
Test your Magento XML files from command line with this tool and see all errors (if any)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/php | |
<?php | |
/** | |
* Debug an XML file | |
* @author Jochem Klaver <j.klaver@drecomm.nl> | |
* @link http://php.net/libxml | |
*/ | |
require('app/Mage.php'); | |
Mage::app(); | |
if (!isset($argv) || !is_array($argv) || !isset($argv[1])) { | |
die("No file given\n"); | |
} | |
$xmlFile = $argv[1]; | |
if (strtolower(substr($xmlFile, -4)) != '.xml') { | |
die("File has no XML file extension\n"); | |
} | |
if (!file_exists($xmlFile)) { | |
die("File does not exist\n"); | |
} | |
if (!is_readable($xmlFile)) { | |
die("File is not readable\n"); | |
} | |
$config = new Varien_Simplexml_Config(); | |
// Set libxml to use internal error | |
libxml_use_internal_errors(true); | |
libxml_clear_errors(); | |
if ($config->loadFile($xmlFile) && count(libxml_get_errors()) == 0) { | |
die("No errors found in XML file '{$xmlFile}'\n"); | |
} else { | |
echo count(libxml_get_errors()) . " errors in XML file '{$xmlFile}'\n"; | |
foreach (libxml_get_errors() as $i => $error) { | |
/* @var $error libXMLError */ | |
echo str_pad(($i+1), 3, ' ', STR_PAD_LEFT) . '. '; | |
switch ($error->level) { | |
case LIBXML_ERR_WARNING: | |
echo "Warning"; | |
break; | |
case LIBXML_ERR_ERROR: | |
echo "Error"; | |
break; | |
case LIBXML_ERR_FATAL: | |
echo "Fatal Error"; | |
break; | |
} | |
echo ' [' . $error->code . ']'; | |
echo ' @ ' . (isset($error->file) && !empty($error->file) ? $error->file . ' on ' : '') | |
. 'line ' . $error->line . ', column ' . $error->column; | |
echo ': ' . trim($error->message); | |
echo "\n"; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment