PHP5 + XML + DOM, il y a t il un bug ou bien je suis vraiment nul ???

izanifu Messages postés 1 Date d'inscription vendredi 14 mai 2004 Statut Membre Dernière intervention 1 mars 2006 - 1 mars 2006 à 10:31
malalam Messages postés 10839 Date d'inscription lundi 24 février 2003 Statut Membre Dernière intervention 2 mars 2010 - 1 mars 2006 à 11:13
Bonjour à tous,
Voila je developpe pour mon travail une classe en php5 basé sur DOM pour pouvoir faire des add/mod/del sur un fichier XML.
Comment cela devrait fonctionner :
- pour un add/mod, il faut passer en parametre le chemin du fichier, le xpath pour trouver où faire l'ajout ou la suppression et les données sous forme d'une chaine au format XML
- pour le del, le chemin et le xpath suffisent.
Le mod fonctionne tres bien, ce qui n'est pas le cas du add et du del.

Que ce passe t il :
si je fais l'enregistrement dans un fichier different de mon fichier source, cela fonctionne tres bien.
Le probleme est quand je veux sauvegarder les modifications dans mon fichier source.
Si j'affiche la chaine que je vais enregistrer juste avant de le faire elle est bonne.
Ensuite je fais mon enregistrement, puis je recharge le fichier et je l'affiche, il est bon.
J'en viens au probleme a proprement parler, lorsque j'ouvre avec un editeur (dreamweaver,editplus,wordpad,...) mon fichier mise à jour, cela ne va plus du tout.

Je m'explique :
- dans le cas d'un add, il a fait deux fois l'ajout demandé
- dans le cas d'un del, il l'a fait deux fois aussi.

Cela fait 1 semaine que je me prends la tete la dessus sans pouvoir comprendre quoi que ce soit...

Au secours, à l'aide, SOS, Please help !!!!!

Voici le code source du truc :
/*****DEBUT CLASSE XML *****/
class xml
{
public function load_file($path) {
$returnValue= simplexml_load_file($path);

return $returnValue;
}
public function load_chaine($chaine) {
$returnValue=simplexml_load_string($chaine);

return $returnValue;
}
public function add($path,$xpath,$data) {
//creation obj DOM
$doc = new DOMDocument;
//charge fichier xml d'origine
$doc->load($path);
//creation obj DOM xpath
$doc_xpath = new DOMXPath($doc);
//recherche par xpath du noeud où ajouter (normalement une seule reponse)
$entries=$doc_xpath->query($xpath);
//on selectionne la reponse
$addme=$entries->item(0);
$pNode=$addme->parentNode;
//conversion de la chaine en obj simplexml
$data=$this->load_chaine($data);
//conversion de l'obj simplexml en obj DOM
$data=dom_import_simplexml($data);
//inclusion de l'obj au document courant
$data=$doc->importNode($data,true);
//ajout des donnees
$pNode->appendChild($data);
//enregistrement du fichier
$this->save($path,$doc->saveXML());
}
public function mod($path,$xpath,$data) {
//creation obj DOM
$doc = new DOMDocument;
//charge fichier xml d'origine
$doc->load($path);
//creation obj DOM xpath
$doc_xpath = new DOMXPath($doc);
//recherche par xpath du noeud a modifier (normalement une seule reponse)
$entries=$doc_xpath->query($xpath);
//on selectionne la reponse
$modme=$entries->item(0);
$pNode=$modme->parentNode;
//conversion de la chaine en obj simplexml
$data=$this->load_chaine($data);
//conversion de l'obj simplexml en obj DOM
$data=dom_import_simplexml($data);
//inclusion de l'obj au document courant
$data=$doc->importNode($data,true);
//modification des donnees
$pNode->replaceChild($data, $modme);
//enregistrement du fichier
$this->save($path,$doc->saveXML());
}
public function del($path,$xpath) {
//creation obj DOM
$doc = new DOMDocument;
//charge fichier xml d'origine
$doc->load($path);
//creation obj DOM xpath
$doc_xpath = new DOMXPath($doc);
//recherche par xpath du noeud a supprimer (normalement une seule reponse)
$entries=$doc_xpath->query($xpath);
//on selectionne la reponse
$delme=$entries->item(0);
$pNode=$delme->parentNode;
//suppression des donnees
$pNode->removeChild($delme);
//enregistrement du fichier
$this->save($path,$doc->saveXML());
//si cette ligne est presente il ne double pas la supprission mais il fait une erreur
//@$pNode->removeChild($delme);
}
private function save($path,$str_xml) {
$fic=fopen($path,"w");
fwrite($fic,$str_xml);
fclose($fic);
}
}
/*****FIN CLASSE XML *****/

/*****DEBUT FICHIER XML *****/
<?xml version="1.0" encoding="iso-8859-1"?>
<MENU>
<ELEMENT>
<TYPE>rep</TYPE>
accueil
<HTML actif ="no"/>
my_mod.png
my_mod_over.png
<OPEN_IN/>
<CONTENT>
<ELEMENT>
<TYPE>1</TYPE>
accueil_user
<HTML actif= "no"/>
gohome.png
gohome_over.png
<OPEN_IN>same</OPEN_IN>
<CONTENT>
accueil

user
</CONTENT>
</ELEMENT>


<ELEMENT>
<TYPE>4</TYPE>
google
<HTML actif ="yes">&lt;b&gt;Google&lt;/b&gt;</HTML>
myelem.png
myelem_over.png
<OPEN_IN>new</OPEN_IN>
<CONTENT>http://www.google.fr/search?client= firefox&amp;hl=fr&amp;q=joelle</CONTENT>
</ELEMENT>
</CONTENT>
</ELEMENT>
<ELEMENT>
<TYPE>rep</TYPE>
accueil
<HTML actif ="no"/>
my_mod.png
my_mod_over.png
<OPEN_IN/>
<CONTENT>
<ELEMENT>
<TYPE>rep</TYPE>
accueil
<HTML actif= "no"/>
my_mod.png
my_mod_over.png
<OPEN_IN/>
<CONTENT/>
</ELEMENT>
<ELEMENT>
<TYPE>rep</TYPE>
accueil
<HTML actif ="no"/>
my_mod.png
my_mod_over.png
<OPEN_IN/>
<CONTENT>
<ELEMENT>
<TYPE>url</TYPE>
google
<HTML actif= "yes">&lt;b&gt;Google&lt;/b&gt;</HTML>
myelem.png
myelem_over.png
<OPEN_IN>new</OPEN_IN>
<CONTENT>http://www.google.fr/search?client =firefox&amp;hl= fr&amp;q=joelle</CONTENT>
</ELEMENT>
</CONTENT>
</ELEMENT>
</CONTENT>
</ELEMENT>
<ELEMENT>
<TYPE>rep</TYPE>
accueil
<HTML actif ="no"/>
my_mod.png
my_mod_over.png
<OPEN_IN/>
<CONTENT>
<ELEMENT>
<TYPE>url</TYPE>
google
<HTML actif= "yes">&lt;b&gt;Google&lt;/b&gt;</HTML>
myelem.png
myelem_over.png
<OPEN_IN>new</OPEN_IN>
<CONTENT>http://www.google.fr/search?client =firefox&amp;hl= fr&amp;q=joelle</CONTENT>
</ELEMENT>
<ELEMENT>
<TYPE>pack</TYPE>
accueil_user
<HTML actif ="no"/>
gohome.png
gohome_over.png
<OPEN_IN>same</OPEN_IN>
<CONTENT>
accueil

admin
</CONTENT>
</ELEMENT>
</CONTENT>
</ELEMENT>
<ELEMENT>
<TYPE>rep</TYPE>
accueil
<HTML actif= "no"/>
my_mod.png
my_mod_over.png
<OPEN_IN/>
<CONTENT/>
</ELEMENT>
</MENU>
/*****FIN FICHIER XML *****/

/*****DEBUT FICHIER TEST *****/
$xml = new xml(); $data"\n\t<ELEMENT>\n\t\t<TYPE>rep</TYPE>\n\t\t joelle \n\t\t<HTML actif"no"/>\n\t\tmy_mod.png
\n\t\tmy_mod_over.png\n\t\t<OPEN_IN/>\n\t\t<CONTENT/>\n\t</ELEMENT>";
//$xml->add("./../packages/accueil/user/menu_tmp.xml",'/MENU/ELEMENT[1]/CONTENT/ELEMENT',$data);
//$xml->mod("./../packages/accueil/user/menu_tmp.xml",'/MENU/ELEMENT[1]/CONTENT/ELEMENT[2]',$data);
$xml->del("./../packages/accueil/user/menu.xml",'/MENU/ELEMENT[1]/CONTENT/ELEMENT[2]');
/*****FIN FICHIER TEST *****/
A voir également:

1 réponse

malalam Messages postés 10839 Date d'inscription lundi 24 février 2003 Statut Membre Dernière intervention 2 mars 2010 25
1 mars 2006 à 11:13
Hello,

suis désolé mais je viens de tester, et personnellement, ça ne me fait qu'un seul ajout.
Niveau objet, je peux savoir pourquoi tu ne stockes pas le chemin, par exemple ?
0
Rejoignez-nous