错误:DOMElement 上非对象
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
与:
您正在执行:
DOMNodelist::item
(请参阅:http://www.php.net/manual/en/domnodelist.item.php),如果在给定索引处找不到元素,则返回NULL。但您并没有检查这种可能性,您只是期望
$p
不为空。尝试添加类似的内容:
尽管如此,如果您确定应该有一个 body 元素,您可能必须检查您的文件路径。
with:
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:
Although, if you're sure that there should be a body element, you'll probably have to check your file paths.
应该是因为你的 DOM 文档中没有
标签。
It should be because there is no
<body>
tag in your DOM Document.