xml - 如何轻松调试布局 Xml 警告/错误?

标签 xml debugging magento xdebug

我被这个错误困住了:

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

Entity: line 46: parser error : Start tag expected, '<' not found  in */lib/Varien/Simplexml/Config.php on line 510

明明是某个Xml文件有问题,但大海捞针实在不容易:)

有什么好的做法吗? 如果可能的话,我想找到一个涉及使用 Xdebug 或一些日志的良好实践。

在 Magento 中经常会出现拼写错误的情况。

最佳答案

我觉得你可以看看这篇文章Dealing with XML errors .

此警告与某些 config.xml 错误有关,因此找出确切文件的可能解决方法是修改。/lib/Varien/Simplexml/Config.php 类。

您应该修改 Varien_Simplexml_Config::loadString() 方法:

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;
}

如果错误与某些布局文件有关(Update.php 第 444 行警告)

您应该以类似的方式修改 Mage_Core_Model_Layout_Update::getFileLayoutUpdatesXml() 方法:

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;
}

现在只需重新加载页面并阅读错误信息。

关于xml - 如何轻松调试布局 Xml 警告/错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13152232/

相关文章:

java - 是否可以为 POI lib 创建的 Word 文档定义 xml 架构?

xml - XSD 总是使用第三方工具生成吗?

php - 使用 FirePHP 输出数组

magento - 使用 Paypal 快速结帐完成付款后如何获取授权ID

php - Zend Framework 无法打开 ssl 套接字 (Abstract.php)

xml - 将 XML 字符串反序列化为对象 VB.NET

java - Jackson xml 和 json 根元素

c++ - free(): invalid pointer error in C++

java - 欧拉项目解决方案 #3 (Java) : Debugging

magento - 集合 : filter by created_at by local time zone?(或 CreatedAtStoreDate)