阅读背景:

在非对象中调用成员函数appendChild()

来源:互联网 

what is wrong with this code ?

这段代码有什么问题?

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("https://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");
}
p



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: