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

Langage PHP Discussion :

Problème avec la fonction fputs


Sujet :

Langage PHP

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre éclairé Avatar de GDMINFO
    Profil pro
    Inscrit en
    Avril 2007
    Messages
    350
    Détails du profil
    Informations personnelles :
    Âge : 45
    Localisation : France

    Informations forums :
    Inscription : Avril 2007
    Messages : 350
    Par défaut Problème avec la fonction fputs
    Bonjour,

    Je suis en train d'élaborer un parser en utilisant Sax en PHP.
    Dans ce cadre, je souhaite recopier des données dans un fichier de sortie.
    L'entête de mon fichier de sortie se copie correctement, mais il n'y a pas moyen de copier autre chose dans ce fichier.

    Voyez vous une raison pour laquelle la fonction fputs ne pourrait - elle pas être utilisée plusieurs fois de suite ?

    Voici une partie du 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
    function startDocument()
    {
        $this->fichier=fopen($nitroS.sbml,a);
        fputs($this->fichier,"<?xml version=\"1.0\" encoding=\"UTF-8\"?>
     
    <graphml xmlns=\"http://graphml.graphdrawing.org/xmlns/graphml\">
     
    xmlns: xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
     
    xsi:schemaLocation=\"http://graphml.graphdrawing.org/xmlns/graphml http:/ /graphml.graphdrawing.org/xmlns/graphml/graphml-attributes-1.0rc.xsd\">
     
    <graph edgedefault=\"undirected\">");
        $this->statebis=0;
        fputs($this ->fichier,"bloup");
    }
    Et bloup n'est pas écrit !

    Merci beaucoup.

  2. #2
    Membre éclairé Avatar de GDMINFO
    Profil pro
    Inscrit en
    Avril 2007
    Messages
    350
    Détails du profil
    Informations personnelles :
    Âge : 45
    Localisation : France

    Informations forums :
    Inscription : Avril 2007
    Messages : 350
    Par défaut
    Après quelques corrections, voici l'intégralité du 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
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    <?php
    header('Content-type: text/plain');
    include('Sax4PHP.php');
    $fsortie = 'nitroS.xml';
    class sbml2graphml extends DefaultHandler
        {
        function startElement($nom, $att)
            {if($nom == 'reaction')
                {
                $this->statebis=1;
                $this->tabReactions[] = $att['name'];
                }
             if($this->statebis==1 and $nom == 'substrate')
                {
                if( !(in_array($att['name'],$this->tabNoeuds)))
                    {
                    $this->tabNoeuds[] = $att['name'];
                    }
                }
             if($this->statebis==1 and $nom == 'product')
                {
                if(!in_array($att['name'],$this->tabNoeuds))
                    {
                    $this->tabNoeuds[] = $att['name'];
                    }
                }
            }
     
        function endElement($nom)
            {
            if($nom == 'reaction')
                {
                $this->statebis=0;
                 }
            }
     
    /* Récupère ce qui est entre deux balises, $cdc contient la chaîne de caractères courante */
        function character($cdc)
            {
            }
     
    function startDocument()
    {
    $this->fichier=fopen($fsortie,w);
    fputs($this->fichier,"<?xml version=\"1.0\" encoding=\"UTF-8\"?>
    <graphml xmlns=\"http://graphml.graphdrawing.org/xmlns/graphml\">
    xmlns: xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
    xsi:schemaLocation=\"http://graphml.graphdrawing.org/xmlns/graphml http://graphml.graphdrawing.org/xmlns/graphml/graphml-attributes-1.0rc.xsd\">
    <graph edgedefault=\"undirected\">");
    $this->tabNoeuds=array();
    $this->statebis=0;
    }
     
     
     
      function endDocument()
    {
    fputs($this->fichier,'</graph></graphml>');
        foreach($this->tabNoeuds as  $this->noeud)
            {
            echo '<node id=',$this->noeud,'>',"\n";
            }
     
    fclose(this->fichier);
    }
        }//fin de la classe
     
    $xml = file_get_contents('nitro.sbml');
    $sax = new SaxParser(new sbml2graphml());
    $sax->parse($xml);
    ?>
    Je n'ai plus de warning, mais par contre plus aucune sortie non plus ! Juste cette erreur quand j'essaie d'afficher le fichier de sortie :

    La page XML ne peut pas être affichée
    Impossible d'afficher l'entrée XML en utilisant la feuille de style XSL. Corrigez l'erreur, puis cliquez sur le bouton Actualiser ou réessayez ultérieurement.


    --------------------------------------------------------------------------------

    Les balises suivantes n'ont pas été fermées : graphml, graph. Erreur de traitement de la ressource http://localhost/ParserG...
    Une idée ?

  3. #3
    Membre éclairé Avatar de GDMINFO
    Profil pro
    Inscrit en
    Avril 2007
    Messages
    350
    Détails du profil
    Informations personnelles :
    Âge : 45
    Localisation : France

    Informations forums :
    Inscription : Avril 2007
    Messages : 350
    Par défaut La librairie
    Etant toujours à la recherche d'une solution, voici la librairie sax4php qui est utilisée :

    <?php
    /**
    * This file contains the code for managing SAX via basic PHP XML Parser functions.
    * (see http://www.php.net/manual/en/ref.xml.php)
    *
    * PHP version 5.1
    *
    * @category XML for PHP
    * @package Sax4PHP
    * @version 0.1
    * @author Emmanuel Desmontils <emmanuel.desmontils@univ-nantes.fr> Original Author
    * @license GNU GPL
    * @copyright (c) 2007 Emmanuel Desmontils
    * @link http://www.desmontils.net
    *
    * This program is free software; you can redistribute it and/or
    * modify it under the terms of the GNU General Public License
    * as published by the Free Software Foundation; either version 2
    * of the License, or (at your option) any later version.
    *
    * This program is distributed in the hope that it will be useful,
    * but WITHOUT ANY WARRANTY; without even the implied warranty of
    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    * GNU General Public License for more details.
    *
    * You should have received a copy of the GNU General Public License
    * along with this program; if not, write to the Free Software
    * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
    *
    */



    /**
    * DefaultHandler Class
    *
    * This class is the default handlers for majors types of events.
    *
    * basic usage:<code>
    * class CompteurFormations extends SaxHandler {
    * function startElement($name, $att) {echo "<start name='$name'/>\n";}
    * function endElement($name) {echo "<end name='$name'/>\n";}
    * function startDocument() {echo '<?xml version="1.0" encoding="ISO-8859-1"?><list>';}
    * function endDocument() {echo '</list>';}
    * }
    * </code>
    *
    * @access public
    */

    class DefaultHandler {
    function __construct() {}
    function __destruct(){}

    //Basic PHP/SAX object to handle events
    final function startElementHandler($sax, $name, $att) {$this->startElement($name, $att);}
    final function endElementHandler($sax, $name) { $this->endElement($name);}
    final function piHandler($sax, $target, $content) { $this->pi($target, $content);}
    final function defaultHandler($sax, $data) { $this->node($data);}
    final function characterHandler($sax, $string) { $this->character($string);}
    final function startDocumentHandler($sax) { $this->startDocument();}
    final function endDocumentHandler($sax) { $this->endDocument();}

    //Java like functions to manage SAX events
    function startElement($name, $att) {}
    function endElement($name) {}
    function pi($target, $content) {}
    function node($data) {}
    function character($string) {}
    function startDocument() {}
    function endDocument(){}
    }



    /**
    * SaxParser Class
    *
    * This class is the main class for parsing an XML string using a SAX Handler.
    *
    * basic usage:<code>
    * $xml = file_get_contents('myFile.xml');
    * $sax = new SaxParser(new mySaxHandler());
    * $sax->parse($xml);
    * </code>
    *
    * @access public
    */

    class SaxParser {
    private $sax;
    private $saxHandler;

    function __construct(DefaultHandler $saxHandler, $encoding = 'UTF-8') {
    $this->saxHandler = $saxHandler;
    $this->sax = xml_parser_create($encoding);
    xml_set_object($this->sax,$saxHandler);
    xml_parser_set_option($this->sax,XML_OPTION_CASE_FOLDING, FALSE);
    $this->setElementHandler('startElementHandler','endElementHandler');
    $this->setDefaultHandler('defaultHandler');
    $this->setCharacterHandler('characterHandler');
    $this->setPIHandler('piHandler');
    }
    function __destruct() {xml_parser_free($this->sax);}

    function setElementHandler($openElementHandler, $closeElementHandler) {
    xml_set_element_handler($this->sax,$openElementHandler,$closeElementHandler);}
    function setDefaultHandler($defaultHandler) {
    xml_set_default_handler($this->sax,$defaultHandler);}
    function setCharacterHandler($characterHandler){
    xml_set_character_data_handler ($this->sax, $characterHandler);}
    function setPIHandler($piHandler){
    xml_set_processing_instruction_handler ($this->sax, $piHandler);}

    function parse($xml) {
    $this->saxHandler->startDocumentHandler($this->sax);
    xml_parse($this->sax,$xml,TRUE);
    $this->saxHandler->endDocumentHandler($this->sax);
    }
    }?>
    Par ailleurs j'ai testé la fonction fputs qui fonctionne parfaitement dans un cadre "classique".

    Si ça vous inspire... n'hésitez pas à poster !

Discussions similaires

  1. Problème avec une fonction date.
    Par kmayoyota dans le forum ASP
    Réponses: 8
    Dernier message: 09/09/2004, 13h33
  2. Problème avec la fonction findfirst ()
    Par Angelico dans le forum Windows
    Réponses: 3
    Dernier message: 05/08/2004, 21h40
  3. [Requete SQL en VBA] Problème avec la fonction FLOOR
    Par zubral dans le forum Langage SQL
    Réponses: 4
    Dernier message: 13/07/2004, 14h24
  4. Problème avec les fonctions
    Par jvachez dans le forum PostgreSQL
    Réponses: 1
    Dernier message: 13/01/2004, 13h06
  5. [Postgresql]Problème avec les fonctions ...
    Par fet dans le forum Requêtes
    Réponses: 4
    Dernier message: 02/10/2003, 10h04

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