Tell me more ×
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It's 100% free, no registration required.

Magento is fairly protective (as it should be) regarding the display of errors. When developer mode is enabled (as it should be during development), the application allows runtime error feedback to bubble up to the user. For XML compilation errors though this feedback is fairly useless:

Fatal error: Uncaught exception 'Exception' with message 'Warning: simplexml_load_string(): Entity: line 4: parser error : XML declaration allowed only at the start of the document in [...]/lib/Varien/Simplexml/Config.php on line 510' in [...]app/code/core/Mage/Core/functions.php on line 245

This results from Varien_Simplexml_Config::loadFile() presenting ::loadString() with a string which cannot be parsed:

public function loadFile($filePath)
{
    if (!is_readable($filePath)) {
        //throw new Exception('Can not read xml file '.$filePath);
        return false;
    }

    $fileData = file_get_contents($filePath);
    $fileData = $this->processFileData($fileData);
    return $this->loadString($fileData, $this->_elementClass);
}

There are several potential solutions, including using libxml_use_internal_errors, but the calling method does not communicate the $filePath param, so context would be lost. One possibility would be to throw a more explicit exception:

public function loadFile($filePath)
{
    if (!is_readable($filePath)) {
        //throw new Exception('Can not read xml file '.$filePath);
        return false;
    }

    $fileData = file_get_contents($filePath);
    $fileData = $this->processFileData($fileData);
    try{
        return $this->loadString($fileData, $this->_elementClass);
    }
    catch (Exception $e){
        Mage::throwException (
            sprintf(
                "%s: error parsing %s:\r\n%s",
                __METHOD__,
                $filePath,
                $e->getMessage()
            )
        );
    }
}

This at least provides output like the following:

Fatal error: Uncaught exception 'Mage_Core_Exception' with message 'Varien_Simplexml_Config::loadFile: error parsing [...]/app/code/local/Some/Example/etc/config.xml: Warning: simplexml_load_string(): Entity: line 4: parser error : XML declaration allowed only at the start of the document in [...]/lib/Varien/Simplexml/Config.php on line 534' in [...]/app/Mage.php on line 594

Are there some advantages/disadvantages/alternate approaches to consider here?

share|improve this question
add comment (requires an account with 50 reputation)

1 Answer

up vote 6 down vote accepted

The approach I always take is a simple one-liner:

find . -type f -name '*.xml' -exec xmllint --noout {} \;

libxml2-utils required though...

share|improve this answer
and shows false positives in the wsi files: namespace error : xmlns:typens: 'urn:{{var wsdl.name}}' is not a valid URI – Alex May 28 at 10:21
Workaround: find . -type f -not -name 'wsi.xml' -not -name 'wsdl.xml' -not -name 'wsdl2.xml' -name '*.xml' -exec xmllint --noout {} \; (feel free to edit the answer) – Alex May 28 at 10:24
For config.xml: I'm a fan of find . -type f -name 'config.xml' -exec xmllint --noout {} \; – benmarks May 31 at 0:41
add comment (requires an account with 50 reputation)

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.