错误:DOMElement 上非对象

发布于 2024-11-07 13:27:13 字数 1341 浏览 0 评论 0原文

foreach ($filePaths as $filePath) {
/*Open a file, run a function to write a new file 
that rewrites the information to meet design specifications */
$fileHandle = fopen($filePath, "r+");

$newHandle = new DOMDocument();
$newHandle->loadHTMLFile( $filePath );
$metaTitle = trim(retrieveTitleText($newHandle));
$pageMeta = array('metaTitle' => $metaTitle, 'pageTitle' => 'Principles of Biology' );
$attributes = retrieveBodyAttributes($filePath);

cleanfile($fileHandle, $filePath);

fclose($fileHandle);
}

function retrieveBodyAttributes($filePath) {
$dom = new DOMDocument;
$dom->loadHTMLFile($filePath);
$p = $dom->getElementsByTagName('body')->item(0);
/*if (!$p->hasAttribute('body')) {
    $bodyAttr[] = array('attr'=>" ", 'value'=>" ");
    return $bodyAttr;
}*/
if ($p->hasAttributes()) {
    foreach ($p->attributes as $attr) {
        $name = $attr->nodeName;
        $value = $attr->nodeValue;
        $bodyAttr[] = array('attr'=>$name, 'value'=>$value);
    }
    return $bodyAttr;
}

}

$filePaths 是一个字符串数组。当我运行代码时,它给我一个调用 hasAttributes 的行的“在非对象上调用成员函数 hasAttributes()”错误。当它没有被注释掉时,我在调用 hasAttribute('body') 的行上得到相同的错误。我在调用 getElementsByTagName 之后就在 $p 上尝试了 var_dump,并且得到了“object (DOMElement) [5]”。好吧,数字发生了变化,因为我同时在多个文件上运行代码,但我不知道这个数字意味着什么。我找不到我做错了什么。

foreach ($filePaths as $filePath) {
/*Open a file, run a function to write a new file 
that rewrites the information to meet design specifications */
$fileHandle = fopen($filePath, "r+");

$newHandle = new DOMDocument();
$newHandle->loadHTMLFile( $filePath );
$metaTitle = trim(retrieveTitleText($newHandle));
$pageMeta = array('metaTitle' => $metaTitle, 'pageTitle' => 'Principles of Biology' );
$attributes = retrieveBodyAttributes($filePath);

cleanfile($fileHandle, $filePath);

fclose($fileHandle);
}

function retrieveBodyAttributes($filePath) {
$dom = new DOMDocument;
$dom->loadHTMLFile($filePath);
$p = $dom->getElementsByTagName('body')->item(0);
/*if (!$p->hasAttribute('body')) {
    $bodyAttr[] = array('attr'=>" ", 'value'=>" ");
    return $bodyAttr;
}*/
if ($p->hasAttributes()) {
    foreach ($p->attributes as $attr) {
        $name = $attr->nodeName;
        $value = $attr->nodeValue;
        $bodyAttr[] = array('attr'=>$name, 'value'=>$value);
    }
    return $bodyAttr;
}

}

$filePaths is an array of strings. When I run the code, it give me a "Call to member function hasAttributes() on non-object" error for the line that calls hasAttributes. When it's not commented out, I get the same error on the line that calls hasAttribute('body'). I tried a var_dump on $p, on the line just after the call to getElementsByTagName, and I got "object (DOMElement) [5]". Well, the number changed because I was running the code on multiple files at once, but I didn't know what the number meant. I can't find what I'm doing wrong.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

唐婉 2024-11-14 13:27:13

与:

$p = $dom->getElementsByTagName('body')->item(0);

您正在执行:DOMNodelist::item(请参阅:http://www.php.net/manual/en/domnodelist.item.php),如果在给定索引处找不到元素,则返回NULL

但您并没有检查这种可能性,您只是期望 $p 不为空。
尝试添加类似的内容:

if ($p instanceof DOMNode) {
  // the hasAttributes code
}

尽管如此,如果您确定应该有一个 body 元素,您可能必须检查您的文件路径。

with:

$p = $dom->getElementsByTagName('body')->item(0);

You are executing: DOMNodelist::item (See: http://www.php.net/manual/en/domnodelist.item.php) which returns NULL if, at the given index, no element is found.

But you're not checking for that possibility, you're just expecting $p to be not null.
Try adding something like:

if ($p instanceof DOMNode) {
  // the hasAttributes code
}

Although, if you're sure that there should be a body element, you'll probably have to check your file paths.

千鲤 2024-11-14 13:27:13

应该是因为你的 DOM 文档中没有 标签。

It should be because there is no <body> tag in your DOM Document.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文