How to Easily Debug Layout Xml Warning in Magento

There is an issue in some Xml files, but really not easy for met to find out the miss spelling in Magento

Posted on April 11, 2016 in Magento

If you found following text in the system.log, it’s very hard to pin point the right file that causes the miss spelling issue.


Warning: simplexml_load_string(): Entity: line 46: parser error : Comment not terminated  in */lib/Varien/Simplexml/Config.php on line 510

The solution is modify the file /lib/Varien/Simplexml/Config.php as following:


  public function loadString($string)
    {
        if (is_string($string)) {
            // Enable internal errors
            libxml_use_internal_errors(true);
            $xml = simplexml_load_string($string, $this->_elementClass);
            if (false === $xml) {
                // Put breakpoint here
                $errors = libxml_get_errors();
            }
            if ($xml instanceof Varien_Simplexml_Element) {
                $this->_xml = $xml;
                return true;
            }
        } else {
            Mage::logException(new Exception('"$string" parameter for simplexml_load_string is not a string'));
        }
        return false;
    }

In case the error is related to some Layout file, you should modify Mage_Core_Model_Layout_Update::getFileLayoutUpdatesXml() method in a similar way:


public function getFileLayoutUpdatesXml($area, $package, $theme, $storeId = null)
{
    if (null === $storeId) {
        $storeId = Mage::app()->getStore()->getId();
    }
    /* @var $design Mage_Core_Model_Design_Package */
    $design = Mage::getSingleton('core/design_package');
    $layoutXml = null;
    $elementClass = $this->getElementClass();
    $updatesRoot = Mage::app()->getConfig()->getNode($area.'/layout/updates');
    Mage::dispatchEvent('core_layout_update_updates_get_after', array('updates' => $updatesRoot));
    $updateFiles = array();
    foreach ($updatesRoot->children() as $updateNode) {
        if ($updateNode->file) {
            $module = $updateNode->getAttribute('module');
            if ($module && Mage::getStoreConfigFlag('advanced/modules_disable_output/' . $module, $storeId)) {
                continue;
            }
            $updateFiles[] = (string)$updateNode->file;
        }
    }
    // custom local layout updates file - load always last
    $updateFiles[] = 'local.xml';
    $layoutStr = '';
    foreach ($updateFiles as $file) {
        $filename = $design->getLayoutFilename($file, array(
            '_area'    => $area,
            '_package' => $package,
            '_theme'   => $theme
        ));
        if (!is_readable($filename)) {
            continue;
        }
        $fileStr = file_get_contents($filename);
        $fileStr = str_replace($this->_subst['from'], $this->_subst['to'], $fileStr);   


        libxml_use_internal_errors(true);
        $fileXml = simplexml_load_string($fileStr, $elementClass);


        if (false === $fileXml) {
            // Put breakpoint here
            $errors = libxml_get_errors();
            $err = array($filename, $errors);
            // error detail and file name will be printed
            Zend_Debug::dump($err);
            die();
        }



        if (!$fileXml instanceof SimpleXMLElement) {
            continue;
        }
        $layoutStr .= $fileXml->innerXml();
    }
    $layoutXml = simplexml_load_string('<layouts>'.$layoutStr.'</layouts>', $elementClass);
    return $layoutXml;
}

Now Just reload the page and read the error info.


comments powered by Disqus