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

时间:2012-10-31 06:14:33

标签: 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很多时候都会拼错拼写错误。

6 个答案:

答案 0 :(得分:16)

我想你可以看一下这篇文章Dealing with XML errors

此警告与某些config.xml错误有关,因此找到确切文件的可能解决方法是mod。 /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;
}

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

答案 1 :(得分:4)

好吧,经过一次精彩的搜索,如果$string是所有CML合并的,那么你几乎没有机会找到哪个标签被关闭了。

我找到了一种方法,在类/lib/Varien/Simplexml/Config.php中你必须修改下一个方法:

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);
    //add this try catch
    try{
        $this->loadString($fileData, $this->_elementClass);
    } catch (Exception $e) {
        var_dump("The error: " . $e->getMessage());
        var_dump("The bad file" . $filePath);
    }

    return $this->loadString($fileData, $this->_elementClass);
}

通过这种方式,我们现在可以看到哪个文件有错误的标签。

答案 2 :(得分:2)

如果您的测试环境是Linux,您还可以使用XMLLint xmllint检查布局xml文件是否存在不一致之后的更改。在实施之前进行检查可以发现大量的错误,并防止出现错误。

答案 3 :(得分:2)

编辑文件app / code / core / Mage / Core / Model / Layout / Update.php并搜索功能&#34; getFileLayoutUpdatesXml&#34;。

添加:

Mage::log(print_r($filename, true));

大约在第442行,之前:

$fileStr = file_get_contents($filename);

答案 4 :(得分:1)

要解决这种情况,请在#489行的同一文件* / lib / Varien / Simplexml / Config.php中

Zend_Debug::dump($filePath);

之后放置$fileData = $this->processFileData($fileData);的已加载XML文件的打印路径

并覆盖函数loadString($ string)

if (is_string($string)) {
    $xml = simplexml_load_string($string, $this->_elementClass);
    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;

替换为

if (is_string($string)) {
    // Enable internal errors
    //Mage::log($string);
    libxml_use_internal_errors(true);
    $xml = simplexml_load_string($string, $this->_elementClass);

    if (false === $xml) {
        // Put breakpoint here
        $errors = libxml_get_errors();
        $err = array($errors);
        // error detail and file name will be printed
        Zend_Debug::dump($string);;
        Zend_Debug::dump($err);
        die();
    }
    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;

这将打印创建问题的路径和错误。

答案 5 :(得分:0)

in

*/lib/Varien/Simplexml/Config.php on line 510

去添加调试行并回显它尝试加载的xml,看看这个字符串有什么问题。