IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Bibliothèques et frameworks PHP Discussion :

[DOM] Fichier écrasé à chaque génération


Sujet :

Bibliothèques et frameworks PHP

  1. #1
    Membre du Club
    Profil pro
    Inscrit en
    Janvier 2008
    Messages
    50
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2008
    Messages : 50
    Points : 67
    Points
    67
    Par défaut [DOM] Fichier écrasé à chaque génération
    bonjour, j'utilise dom pour parser un document en xml.
    le problème est que à chaque nouvel article que je rentre, l'ancien est écrasé

    voici mon code :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    $doc = new DOMDocument('1.0', 'utf-8');
     
    $element = $doc->createElement('content','');
    $doc->appendChild($element);
     
    $element2 = $doc->createElement('article','');
    $element->appendChild($element2);
     
    $param1 = $doc->createElement('headline',$titre);
    $element2->appendChild($param1);
     
    $param2 = $doc->createElement('date',$dateCreation);
    $element2->appendChild($param2);
     
    $param3 = $doc->createElement('copy_intro','');
    $element2->appendChild($param3);
    $paramdata = $doc->createCDATASection($texte_intro);
    $param3->appendChild($paramdata);
     
    $param4 = $doc->createElement('image_thumb',"images/stories/articles_thumb/".$photo);
    $element2->appendChild($param4);
     
    $param5 = $doc->createElement('article_link','index.php?option=com_newspaper&article='.$id.'&lang=fr');
    $element2->appendChild($param5);
     
    $doc->save("../content.xml");
    je voudrais recréer un nouvel article dans <content></content>
    merci pour votre aide

    ps voici mon xml au cas où :
    Code XML : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    <?xml version="1.0" encoding="utf-8"?>
    <content>
      <article>
        <headline>Le sport se la joue-t-il vert ? (2)</headline>
        <date>2009-02-03 00:00:00</date>
        <copy_intro>
        <![CDATA[Deuxième volet de notre série consacrée aux sports*et à*l’environnement. Après le football et la Formule 1, place aux sports d’hiver et aux petites balles blanches et jaunes.<br />]]>
        </copy_intro>
        <image_thumb>images/stories/articles_thumb/Sport-green.jpg</image_thumb>
        <article_link></article_link>
      </article>
    </content>

  2. #2
    Expert éminent sénior

    Profil pro
    Inscrit en
    Juin 2002
    Messages
    6 152
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2002
    Messages : 6 152
    Points : 17 778
    Points
    17 778
    Par défaut
    Il faut alors charger l'ancien fichier au lieu de recréer un arbre vide :
    Code PHP : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    define('IOFILE', '../content.xml');
     
    $doc = new DomDocument;
    if (is_file(IOFILE)) {
        $doc->load(IOFILE);
        $element = $doc->documentElement;
    } else {
        $element = $doc->createElement('content','');
        $doc->appendChild($element);
    }
     
    $element2 = $doc->createElement('article','');
    $element->appendChild($element2);
     
    $param1 = $doc->createElement('headline',$titre);
    $element2->appendChild($param1);
     
    $param2 = $doc->createElement('date',$dateCreation);
    $element2->appendChild($param2);
     
    $param3 = $doc->createElement('copy_intro','');
    $element2->appendChild($param3);
    $paramdata = $doc->createCDATASection($texte_intro);
    $param3->appendChild($paramdata);
     
    $param4 = $doc->createElement('image_thumb',"images/stories/articles_thumb/".$photo);
    $element2->appendChild($param4);
     
    $param5 = $doc->createElement('article_link','index.php?option=com_newspaper&amp;article='.$id.'&amp;lang=fr');
    $element2->appendChild($param5);
     
    $doc->save(IOFILE);
    (non testé)


  3. #3
    Membre du Club
    Profil pro
    Inscrit en
    Janvier 2008
    Messages
    50
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2008
    Messages : 50
    Points : 67
    Points
    67
    Par défaut
    merci beaucoup jules ca marche enfin presque j'ai ce message d'erreur :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
    Warning: DOMDocument::load() [domdocument.load]: EntityRef: expecting ';' in file:///C:/xampp/htdocs/ecofaubourgs/content.xml, line: 2 in C:\xampp\htdocs\ecofaubourgs\administrator\components\com_newspaper\models\newspaper.php on line 283
     
    Fatal error: Call to a member function appendChild() on a non-object in C:\xampp\htdocs\ecofaubourgs\administrator\components\com_newspaper\models\newspaper.php on line 291

  4. #4
    Expert éminent sénior

    Profil pro
    Inscrit en
    Juin 2002
    Messages
    6 152
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2002
    Messages : 6 152
    Points : 17 778
    Points
    17 778
    Par défaut
    Mais ça correspond à quoi ces messages d'erreur ?

    Ici par exemple :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    $param5 = $doc->createElement('article_link','index.php?option=com_newspaper&article='.$id.'&amp;lang=fr');
    Le & précédent le paramètre article est censé être &amp;. Il y a visiblement une autre erreur de ce genre dans votre code.

  5. #5
    Membre du Club
    Profil pro
    Inscrit en
    Janvier 2008
    Messages
    50
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2008
    Messages : 50
    Points : 67
    Points
    67
    Par défaut
    en fait quand g posté j'ai retiré une portion de la ligne par mesure de securité

    voila la ligne entiere :

    $param5 =$doc->createElement('article_link','index.php?option=com_newspaper&amp;view=newspaper&amp;Itemid=9&amp;article='.$id.'&amp;lang=fr');

    ce qui est bizarre c'est que le code marchais avant...

  6. #6
    Expert éminent sénior

    Profil pro
    Inscrit en
    Juin 2002
    Messages
    6 152
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2002
    Messages : 6 152
    Points : 17 778
    Points
    17 778
    Par défaut
    Pouvez-vous nous donner la ligne 2 du fichier XML généré et la ligne 291 de votre script ? Bien que la dernière pourrait être éliminée en résolvant la première.

  7. #7
    Membre du Club
    Profil pro
    Inscrit en
    Janvier 2008
    Messages
    50
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2008
    Messages : 50
    Points : 67
    Points
    67
    Par défaut
    euh je viens juste de trouver je devais aussi corriger le fichier xml manuellement.
    en tout cas merci beaucoup ca m'a beaucoup aidé

  8. #8
    Membre du Club
    Profil pro
    Inscrit en
    Janvier 2008
    Messages
    50
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2008
    Messages : 50
    Points : 67
    Points
    67
    Par défaut
    derniere petite chose si je veux que ce fichier xml me serve pourt un flux rss,
    j'ai essayé d'integré la balise rss dans ta condition mais ca ne marche pas

    voici mon code :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
     
    $doc2 = new DomDocument;
     
    if (is_file(IOFILE2)) {
        $doc2->load(IOFILE2);
        $element = $doc2->documentElement;
    } else {
     
    	$elementrss = $doc2->createElement('RSS','');
        $doc2->appendChild($elementrss);
     
    	$element = $doc2->createElement('content','');
        $element->appendChild($elementrss);
     
    }
     
     
    $element2 = $doc2->createElement('article','');
    $element->appendChild($element2);
     
    $param1 = $doc2->createElement('headline',$titre);
    $element2->appendChild($param1);
     
    $param2 = $doc2->createElement('date',$dateCreation);
    $element2->appendChild($param2);
     
    $param3 = $doc2->createElement('copy_intro','');
    $element2->appendChild($param3);
    $paramdata = $doc2->createCDATASection($texte_intro);
    $param3->appendChild($paramdata);
     
    $param4 = $doc2->createElement('image_thumb',"images/stories/articles_thumb/".$photo);
    $element2->appendChild($param4);
     
    $param5 = $doc2->createElement('article_link','index.php?option=com_newspaper&amp;view=newspaper&amp;Itemid=9&amp;article='.$id.'&amp;lang=fr');
    $element2->appendChild($param5);			 // index.php?option=com_newspaper&view=newspaper&Itemid=9&article=88&lang=fr
     
    $doc2->save(IOFILE2);
    ou est mon erreur?

  9. #9
    Expert éminent sénior

    Profil pro
    Inscrit en
    Juin 2002
    Messages
    6 152
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2002
    Messages : 6 152
    Points : 17 778
    Points
    17 778
    Par défaut
    Un flux RSS suit une grammaire prédéfinie (cf spécifications 1.0 et 2.0, à moins d'utiliser un namespace pour les informations additionnelles). Le simple ajout d'une racine rss ne suffit pas à l'être.

    Sinon, l'élément rss ne sera ici pas créé si le fichier est déjà existant. De même que les insertions ne se feraient pas dans la partie content. Il faudrait donc modifier la partie if :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    if (is_file(IOFILE2)) {
        $doc2->load(IOFILE2);
        $element = $doc2->documentElement->getElementsByTagName('content')->item(0); // Pour être sûr
    }
    Après avoir manuellement rajouter cette racine.

    (au cas où, un tutoriel PHP/RSS : Création d'un système RSS pour vos news avec PHP 4 et 5 )

  10. #10
    Membre du Club
    Profil pro
    Inscrit en
    Janvier 2008
    Messages
    50
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2008
    Messages : 50
    Points : 67
    Points
    67
    Par défaut
    salut julp et merci de ton aide, j'ai bien, consulté les liens que tu m'as passé.
    mais voila ,j'ai un autre problème qui dépasse mes compétences;

    dans le cas ou le fichier iofile n'existe pas, tout se passe bien, mais quand il existe et que je veux rajouter des info a la suite des précédentes, j'ai le droit a un message d'erreur de 4 lignes :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    Fatal error: Uncaught exception 'DOMException' with message 'Wrong Document Error' in C:\xampp\htdocs\ecofaubourgs\administrator\components\com_newspaper\models\newspaper.php:349 Stack trace: #0 C:\xampp\htdocs\ecofaubourgs\administrator\components\com_newspaper\models\newspaper.php(349): DOMNode->appendChild(Object(DOMElement)) #1 C:\xampp\htdocs\ecofaubourgs\administrator\components\com_newspaper\controllers\newspaper.php(30): NewspaperModelnewspaper->convert_xml() #2 C:\xampp\htdocs\ecofaubourgs\libraries\joomla\application\component\controller.php(236): NewspaperControllernewspaper->save() #3 C:\xampp\htdocs\ecofaubourgs\administrator\components\com_newspaper\admin.newspaper.php(20): JController->execute('save') #4 C:\xampp\htdocs\ecofaubourgs\libraries\joomla\application\component\helper.php(162): require_once('C:\xampp\htdocs...') #5 C:\xampp\htdocs\ecofaubourgs\administrator\includes\application.php(130): JComponentHelper->renderComponent('com_newspaper') #6 C:\xampp\htdocs\ecofaubourgs\administrator\index.php(67): in C:\xampp\htdocs\ecofaubourgs\administrator\components\com_newspaper\models\newspaper.php on line 349
    et voici le code :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
     
    if (is_file(IOFILE2)) {
        $doc2->load(IOFILE2);
        $element = $doc2->documentElement;//->getElementsByTagName('content')->item(0); // Pour être sûr
    	echo"on est dans le if";
    }
    else {
     
    	$elementrss = $doc2->createElement('rss','');
    	$elementrss->setAttribute("version","2.0");
     
        $doc2->appendChild($elementrss);
     
    	echo "on est dans le else";
    	$element = $doc2->createElement('channel','');
    	$elementrss->appendChild($element); 
     
    	$paramtitreprincipal=$doc2->createElement('title',"Flux rss Ecofaubourgs");
    	$element->appendChild($paramtitreprincipal);
     
    	$paramlink=$doc2->createElement('link',"http://www.ecofaubourgs.com");
    	$element->appendChild($paramlink);
     
    	$paramdescription=$doc2->createElement('description',"La ville est un espace naturel");
     
    	$param4 = $doc2->createElement('image',"images/stories/articles_thumb/".$photo);
    	$element->appendChild($param4);
     
     
    $element2 = $doc2->createElement('item','');
    $element->appendChild($element2);
     
     
    	}
     
     
     
    $param1 = $doc2->createElement('title',$titre);
    $element2->appendChild($param1);
     
    $param5 = $doc2->createElement('link','index.php?option=com_newspaper&amp;view=newspaper&amp;Itemid=9&amp;article='.$id.'&amp;lang=fr');
    $element2->appendChild($param5);			 // index.php?option=com_newspaper&view=newspaper&Itemid=9&article=88&lang=fr
     
    //$param2 = $doc2->createElement('date',$dateCreation);
    //$element2->appendChild($param2);
     
    $param3 = $doc2->createElement('description','');
    $element2->appendChild($param3);
    $paramdata = $doc2->createCDATASection($texte_intro);
    $param3->appendChild($paramdata);
     
     $doc2->save(IOFILE2);

  11. #11
    Expert éminent sénior

    Profil pro
    Inscrit en
    Juin 2002
    Messages
    6 152
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2002
    Messages : 6 152
    Points : 17 778
    Points
    17 778
    Par défaut
    La ligne 349, laquelle est-ce ?

    Visiblement, une variable, $element2, n'est pas initialisée dans le cas où le fichier existe. Mais je ne pense pas qu'elle soit à l'origine de cette erreur, le message serait probablement différent (plutôt : call to a member function ... on a non-object ...). Un autre arbre à côté ?

Discussions similaires

  1. Réponses: 6
    Dernier message: 28/02/2009, 15h57
  2. Fichier xsd et génération des fichiers java
    Par Llaur76 dans le forum Services Web
    Réponses: 2
    Dernier message: 09/08/2007, 16h29
  3. [XML][DOM] fichier xml thailandais
    Par tigrou2405 dans le forum APIs
    Réponses: 3
    Dernier message: 13/10/2006, 15h36
  4. Réponses: 11
    Dernier message: 01/03/2006, 10h32
  5. récupérer fichier écrasé
    Par alainpeniche dans le forum Autres Logiciels
    Réponses: 3
    Dernier message: 10/10/2005, 10h46

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo