Php 对中的非对象调用成员函数appendChild()
这个代码有什么问题Php 对中的非对象调用成员函数appendChild(),php,xml,dom,rss,Php,Xml,Dom,Rss,这个代码有什么问题 public function openXML() { // Ouverture du fichier $file = new DOMDocument(); $file->load("lastitems.xml"); //On retourne le fichier return $file; } public function createXML() { // Création du fichier en mémo
public function openXML()
{
// Ouverture du fichier
$file = new DOMDocument();
$file->load("lastitems.xml");
//On retourne le fichier
return $file;
}
public function createXML()
{
// Création du fichier en mémoire
$file = new DOMDocument("1.0");
// Création du noeud racine
$root = $file->createElement("rss"); //On crée l élément racine
$root->setAttribute("version", "2.0"); //On lui ajoute l attribut version (2.0)
$root = $file->appendChild($root); //On insère la racine dans le document
// Création du noeud channel
$element_channel = $file->createElement("channel");//On crée un élément channel
$element_channel->setAttribute("id", "lastitem"); //On donne un attribut id à notre channel
$element_channel = $root->appendChild($element_channel);//On ajoute cet élément à la racine
// Création du noeud description
$element_description = $file->createElement("description");//On crée un élément description
$element_description = $element_channel->appendChild($element_description);//On ajoute cet élément au channel
// Création du texte pour le noeud description
$texte_description = $file->createTextNode("LAST 10 ITEMS IN VSOOK"); //On crée un texte
$texte_description = $element_description->appendChild($texte_description); //On insère ce texte dans le noeud description
// Création du noeud link et ajout du texte à l élément
$element_link = $file->createElement("link");
$element_link = $element_channel->appendChild($element_link);
$texte_link = $file->createTextNode("http://www.mysite.com/");
$texte_link = $element_link->appendChild($texte_link);
// Création du noeud title et ajout du texte à l élément
$element_title = $file->createElement("title");
$element_title = $element_channel->appendChild($element_title);
$texte_title = $file->createTextNode("RSS ITEMS");
$texte_title = $element_title->appendChild($texte_title);
//On retourne le fichier XML
return $file;
}
public function addOneNews($file, $title, $timestamp, $author)
{
//On récupère le channel
$element_channel = $file->getElementById("lastitem");
// Création du noeud item
$element_item = $file->createElement("item");
$element_item = $element_channel->appendChild($element_item);
// Création du noeud title et ajout du texte à l élément
$element_title = $file->createElement("title");
$element_title = $element_item->appendChild($element_title);
$texte_title = $file->createTextNode($title);
$texte_title = $element_title->appendChild($texte_title);
// Création du noeud link et ajout du texte à l élément
$element_link = $file->createElement("link");
$element_link = $element_item->appendChild($element_link);
$texte_link = $file->createTextNode("Lien vers la news");
$texte_link = $element_link->appendChild($texte_link);
// Création du noeud pubDate et ajout du texte à l élément
$element_date = $file->createElement("pubDate");
$element_date = $element_item->appendChild($element_date);
$texte_date = $file->createTextNode($date("d/m/Y H:i",$timestamp));
$texte_date = $element_date->appendChild($texte_date);
// Création du noeud author et ajout du texte à l élément
$element_author = $file->createElement("author");
$element_author = $element_item->appendChild($element_author);
$texte_author = $file->createTextNode($author);
$texte_author = $element_author->appendChild($texte_author);
}
public function saveXML($file)
{
//Sauvegarde du fichier
$file->save("lastitems.xml");
}
使用时,我会收到以下错误:
致命错误:对非对象调用成员函数appendChild()
在C:\wamp\www\mysite\includes\querys.php的第1604行
第1604行是:
$file->getElementById(“lastitem”)
这只是一个创建和生成XML RSS文件的简单函数。好吧,如果不知道行号是什么,确实不可能确定,但我打赌是这一行:
$file->getElementById("lastitem");
是否确实存在具有id=“lastitem”
的元素
除非您发表评论并告诉我们实际行号,否则我怀疑您不会得到更好的结果。是的,我们确切知道哪一行是1604,您能指出哪一行是1604吗?这一行:$file->getElementById(“lastitem”);是的,您选择了正确的行$文件->getElementById(“lastitem”);id=“lastitem”存在php.net帮助中的此注释,但需要在我的函数解决方案中进行一些调整,即
//Création du noeud channel$element\u channel=$file->createElement(“channel”)//在crée unélément channel$element_channel=$root->appendChild($element_channel)//在ajoute cetélémentála racine$element_id=$file->createAttribute(“xml:id”);$element\u id=$element\u channel->appendChild($element\u id);$element_idt=$file->createTextNode(“lastitem”)//在donne un attribute idánotre通道$element\u idt=$element\u id->appendChild($element\u idt);$element\u idt=$element\u channel->setIdAttribute(“xml:id”,true)代码>