消息“未找到错误”的未捕获异常“ DOMException” [英] Uncaught exception 'DOMException' with message 'Not Found Error'

查看:87
本文介绍了消息“未找到错误”的未捕获异常“ DOMException”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我正在为CMS写一个模板系统,我希望有一个模块化的结构,其中涉及到人们在其中放置标签的情况,例如:

Bascially I'm writing a templating system for my CMS and I want to have a modular structure which involves people putting in tags like:

<模块名称= news /> < include name = anotherTemplateFile /> 在我的php中,并替换为动态html。

<module name="news" /> or <include name="anotherTemplateFile" /> which I then want to find in my php and replace with dynamic html.

此处有人将我指向DOMDocument,但我已经遇到了问题。

Someone on here pointed me towards DOMDocument, but I've already come across a problem.

我试图在模板中找到所有< include /> 标记,并用一些简单的html替换它们。这是我的模板代码:

I'm trying to find all <include /> tags in my template and replace them with some simple html. Here is my template code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>CMS</title>
<include name="head" />
</head>

<body>

<include name="header" />

<include name="content" />

<include name="footer" />

</body>
</html>

这是我的PHP:

$template = new DOMDocument();
$template->load("template/template.tpl");

foreach( $template->getElementsByTagName("include") as $include ) {
    $element = '<input type="text" value="'.print_r($include, true).'" />';
    $output = $template->createTextNode($element);
    $template->replaceChild($output, $include);
}

echo $template->saveHTML();

现在,我收到致命错误未捕获的异常'DOMException',消息为'找不到错误'

Now, I get the fatal error Uncaught exception 'DOMException' with message 'Not Found Error'.

我已经查过了,这似乎是因为我的< include /> 标记不一定是 $ template 的直接子代,而不替换它们。

I've looked this up and it seems to be that because my <include /> tags aren't necessarily DIRECT children of $template its not replacing them.

我如何独立于血统来替换它们?

How can I replace them independently of descent?

谢谢

汤姆

编辑

基本上我有各种各样的想法。如果我为PHP做类似的事情,我会看到它正在尝试做我想做的事情:

Basically I had a brainwave of sorts. If I do something like this for my PHP I see its trying to do what I want it to do:

$template = new DOMDocument();
    $template->load("template/template.tpl");

    foreach( $template->getElementsByTagName("include") as $include ) {
        $element = '<input type="text" value="'.print_r($include, true).'" />';
        $output = $template->createTextNode($element);
        // this line is different:
        $include->parentNode->replaceChild($output, $include);
    }

    echo $template->saveHTML();

不过,它似乎只改变了< body> ...当我有3时::/

However it only seems to change 1 occurence in the <body> of my HTML... when I have 3. :/

推荐答案

这是您的问题DOMDocument-> load,尝试

This is a problem with your DOMDocument->load, try

$template->loadHTMLFile("template/template.tpl"); 

但是您可能需要给它加上.html扩展名。

But you may need to give it a .html extension.

这正在寻找html或xml文件。另外,每当您将DOMDocument与html一起使用时,在加载调用之前使用 libxml_use_internal_errors(true); 是一个好主意。

this is looking for a html or an xml file. also, whenever you are using DOMDocument with html it is a good idea to use libxml_use_internal_errors(true); before the load call.

好的工作方式:

foreach( $template->getElementsByTagName("include") as $include ) {
     if ($include->hasAttributes()) {
     $includes[] = $include;
     }
     //var_dump($includes);
 }
 foreach ($includes as $include) {
            $include_name = $include->getAttribute("name");
        $input = $template->createElement('input');
$type = $template->createAttribute('type');
$typeval = $template->createTextNode('text');
$type->appendChild($typeval);
$input->appendChild($type);
$name = $template->createAttribute('name');
$nameval = $template->createTextNode('the_name');
$name->appendChild($nameval);
$input->appendChild($name);
$value = $template->createAttribute('value');
$valueval = $template->createTextNode($include_name);
$value->appendChild($valueval);
$input->appendChild($value);
if ($include->getAttribute("name") == "head") {
       $template->getElementsByTagName('head')->item(0)->replaceChild($input,$include);
}
else {
        $template->getElementsByTagName("body")->item(0)->replaceChild($input,$include);
}
        }
        //$template->load($nht);
        echo $template->saveHTML();

这篇关于消息“未找到错误”的未捕获异常“ DOMException”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆