bonjour,
voilà un code simple permettant de créer un fichier xml simple (qui fonctionne bien )
j'utilise la version :php4.1.2-7 ( je suis obligée)
<?php /** * Topic: Create XML files using PHP DOM-XML * */ // create the xml processing instruction $doc = new_xmldoc("1.0" ); $filename="stock"; $filedate = strftime("%y%m%d") ; // create a root node and set an attribute $root = $doc->add_root("racine" ); $date = strftime("%d/%m/%C%y") ; $dateNode = $root-> new_child("date", "$date"); $officename="office13"; $code_office = "A013"; // create a child node $officeNode = $root->new_child("office", ""); // set an attribute to the child node $officeNode->set_attribute("name", "$officename"); // create a child node and add content $officeNode->set_attribute("code_office", "$code_office"); $qte="55421"; $code_article="cf10"; $stockNode1 = $officeNode->new_child("stock", "$qte" ); $stockNode1->set_attribute("code_article","$code_article"); $stockNode2 = $officeNode->new_child("stock", "$qte" ); $stockNode2->set_attribute("code_article","$code_article"); // write to file $fp = fopen($filename.$filedate.".xml", "w+" ); fwrite($fp, $doc->dumpmem(), strlen($doc->dumpmem() )); fclose($fp); ?>
j'ai réussi à crée ce simple fichier , maintenant j'ai essyé d'extraire mes données depuis une BDD postgres , je n'arrive pas à ajouter un attribut au dernier fils ,
je reçois l'erreur suivante :
Citation:
Fatal error: Call to a member function on a non-object in /var/www/html/xml/fileXml.php
voilà tout le code que j'utilise :
<? /* Topic: Create XML files using PHP DOM-XML */ // create the xml processing instruction $doc = new_xmldoc("1.0" ); $filename="stock"; $filedate = strftime("%y%m%d") ; global $i ; $root = $doc->add_root("racine" ); date = strftime("%d/%m/%C%y") ; $dbName = "mabase"; $dbUser = "nobody"; $dbPass = "nobody"; $host = "127.0.0.1"; $port = "5432"; $cnxString = "host=".$host." port=".$port." dbname=".$dbName." user=".$dbUser." password=".$dbPass; $pgsql_conn = pg_connect($cnxString); if ($pgsql_conn) { echo "Connexion reussie : \n"; $query = "SELECT office_id , office_name FROM office where office_id like 'A%'"; $result = pg_exec($query) or die('echec requete : ' . pg_last_error()); $cpt = 0; $i = 1 ; while ($i>0 && $line = pg_fetch_array($result)) { $line = pg_fetch_array($result); foreach ($line as $col_value) { $officename=$line[1]; $code_office = $line[0]; } // create a child node $officeNode.$i= $root->new_child("office", ""); // set an attribute to the child node $officeNode.$i->set_attribute("name", "$officename"); // create a child node and add content $officeNode.$i->set_attribute("code_office", "$code_office"); $resultstock = get_result_stock($code_office); $j = 0; while ($linestock = pg_fetch_array($resultstock)) { echo " i = ".$i."et cpt ".$cpt." et j = ".$j; $code_article = $linestock[0]; $start = $linestock[1]; $end = $linestock[2]; $qte=$bstart; echo "stockNode".$i.$j ; // $stockNode.$i.$j->set_attribute("code_article",$code_article); $stockobj = $stockNode.$i.$j ; $stockNode.$i.$j = $officeNode.$i->new_child("stock", "$qte" ); $stockNode.$i.$j ->set_attribute("code_article","11111"); $j++; }//end while $i++; } // write to file $fp = fopen($filename.$filedate.".xml", "w+" ); fwrite($fp, $doc->dumpmem(), strlen($doc->dumpmem() )); fclose($fp); // Ferme la connexion pg_close($pgsql_conn); } else { echo pg_last_notice($pgsql_conn); exit; } echo "\n fin du programme"; function get_result_stock ($code_office) { $stockquery = "SELECT category_id , start , end from stock where op_type='1' and office_id='$code_office'"; $resultstock = pg_exec($stockquery) or die(); return $resultstock ; } ?>
la ligne en rouge est source de l'erreur !
si je la commente tout se passe bien , mais je dois ajouter cet attribut
vraiment je plante