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 :

Parsing en PHP non concluant ....


Sujet :

Langage PHP

  1. #1
    Membre du Club
    Profil pro
    Inscrit en
    Octobre 2004
    Messages
    109
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2004
    Messages : 109
    Points : 48
    Points
    48
    Par défaut Parsing en PHP non concluant ....
    Je possède un fichier XML que je parse. Ce fichier possède des "é" et autres "à" ou "è". Le parsing de mon fichier s'effectue à l'aide de la function PHP xml_parse.
    J'ai bien dit à mon parser qu'il faut travailler en iso-88-59-1
    Et mon fichier xml comporte en entete :
    <?xml version="1.0" encoding="ISO-8859-1"?>

    Mais le parsing de mon fichier donne l'erreur suivante :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    XmlParse error: undefined entity at line 5
    Ma question : Ou définir ces "entity" é è et à ?
    Comment prendre en compte ces spécifités de langue française ?

  2. #2
    Membre expert Avatar de KiLVaiDeN
    Profil pro
    Inscrit en
    Octobre 2003
    Messages
    2 860
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2003
    Messages : 2 860
    Points : 3 444
    Points
    3 444
    Par défaut
    Bonjour,

    Pour que le fichier XML soit conforme, il faut utiliser les htmlentities pour les caractères spéciaux, par exemple &eacute; et autres &egrave;

  3. #3
    Membre du Club
    Profil pro
    Inscrit en
    Octobre 2004
    Messages
    109
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2004
    Messages : 109
    Points : 48
    Points
    48
    Par défaut
    J'ai bein essayé cela mais ca fait le même résultat
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
    XmlParse error: undefined entity at line 4
    [quote]
     :( [/quote]

  4. #4
    Membre expert Avatar de KiLVaiDeN
    Profil pro
    Inscrit en
    Octobre 2003
    Messages
    2 860
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2003
    Messages : 2 860
    Points : 3 444
    Points
    3 444
    Par défaut
    Peux-tu poster les premières lignes de ton fichier XML ?

    J'ai souvent utilisé des XML avec des htmlentities : aucun problème.

    Voici l'entête à utiliser :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    <?xml version="1.0" encoding="iso-8859-1"?>

  5. #5
    Membre du Club
    Profil pro
    Inscrit en
    Octobre 2004
    Messages
    109
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2004
    Messages : 109
    Points : 48
    Points
    48
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
     
    <?xml version="1.0" encoding="ISO-8859-1" ?>
    <doc>
    	<module id="vet">
    		<item  class="xv_titre" libelle="Monsieur" libelle_court="M." formule="Je vous prie de croire, Monsieur, en mes salutations distinguées"/>
    		<item class="xv_titre" libelle="Docteur" libelle_court="Dc" formule="Je vous prie de croire, Chère confrère, en mes salutations distinguées"/>
    	</module>
    </doc>
    Sans é mon parser deroule corretment. Ca peux être le parser PHP mal adapé ?

    Voici le code PHP du parser utilisé avec un $parser->useIsoEncoding();

    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
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
     
     
    <?php
    // $Id: saxparser.php,v 1.11.2.1 2005/06/29 17:03:34 mithyt2 Exp $
    /*******************************************************************************
        Location: <b>xml/SaxParser.class</b><br>
         <br>
        Provides basic functionality to read and parse XML documents.  Subclasses
        must implement all the their custom handlers by using add* function methods.
        They may also use the handle*() methods to parse a specific XML begin and end
        tags, but this is not recommended as it is more difficult.<br>
        <br>
        Copyright &copy; 2001 eXtremePHP.  All rights reserved.<br>
        <br>
        @author Ken Egervari<br>
    *******************************************************************************/
     
    class SaxParser
    {
        var $level;
        var $parser;
     
        var $isCaseFolding;
        var $targetEncoding;
     
        /* Custom Handler Variables */
        var $tagHandlers = array();
     
        /* Tag stack */
        var $tags = array();
     
        /* Xml Source Input */
        var $xmlInput;
     
        var $errors = array();
     
        /****************************************************************************
            Creates a SaxParser object using a FileInput to represent the stream
            of XML data to parse.  Use the static methods createFileInput or
            createStringInput to construct xml input source objects to supply
            to the constructor, or the implementor can construct them individually.
        ****************************************************************************/
        function SaxParser(&$input)
        {
            $this->level = 0;
            $this->parser = xml_parser_create('UTF-8');
            xml_set_object($this->parser, $this);
            $this->input =& $input;
            $this->setCaseFolding(false);
            $this->useUtfEncoding();
            xml_set_element_handler($this->parser, 'handleBeginElement','handleEndElement');
            xml_set_character_data_handler($this->parser, 'handleCharacterData');
            xml_set_processing_instruction_handler($this->parser, 'handleProcessingInstruction');
            xml_set_default_handler($this->parser, 'handleDefault');
            xml_set_unparsed_entity_decl_handler($this->parser, 'handleUnparsedEntityDecl');
            xml_set_notation_decl_handler($this->parser, 'handleNotationDecl');
            xml_set_external_entity_ref_handler($this->parser, 'handleExternalEntityRef');
        }
     
        /*---------------------------------------------------------------------------
            Property Methods
        ---------------------------------------------------------------------------*/
     
        function getCurrentLevel()
        {
            return $this->level;
        }
     
        /****************************************************************************
            * @param $isCaseFolding
            * @returns void
        ****************************************************************************/
        function setCaseFolding($isCaseFolding)
        {
            assert(is_bool($isCaseFolding));
     
            $this->isCaseFolding = $isCaseFolding;
            xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, $this->isCaseFolding);
        }
     
        /****************************************************************************
            * @returns void
        ****************************************************************************/
        function useIsoEncoding()
        {
            $this->targetEncoding = 'ISO-8859-1';
            xml_parser_set_option($this->parser, XML_OPTION_TARGET_ENCODING, 'ISO-8859-1');
        }
     
        /****************************************************************************
            * @returns void
        ****************************************************************************/
        function useAsciiEncoding()
        {
            $this->targetEncoding = 'US-ASCII';
            xml_parser_set_option($this->parser, XML_OPTION_TARGET_ENCODING, $this->targetEncoding);
        }
     
        /****************************************************************************
            * @returns void
        ****************************************************************************/
        function useUtfEncoding()
        {
            $this->targetEncoding = 'UTF-8';
            xml_parser_set_option($this->parser, XML_OPTION_TARGET_ENCODING, $this->targetEncoding);
        }
     
        /****************************************************************************
            Returns the name of the xml tag being parsed
            * @returns string
        ****************************************************************************/
        function getCurrentTag()
        {
            return $this->tags[count($this->tags) - 1];
        }
     
        function getParentTag()
        {
            if (isset($this->tags[count($this->tags) - 2])) {
                return $this->tags[count($this->tags) - 2];
            }
            return false;
        }
     
     
     
        /*---------------------------------------------------------------------------
            Parser methods
        ---------------------------------------------------------------------------*/
     
        /****************************************************************************
            * @returns void
        ****************************************************************************/
        function parse()
        {
            xml_parser_set_option($this->parser, XML_OPTION_TARGET_ENCODING, 'ISO-8859-1');
            if (!is_resource($this->input)) {
                if (!xml_parse($this->parser, $this->input)) {
                    $this->setErrors($this->getXmlError());
                    return false;
                }
                //if (!$fp = fopen($this->input, 'r')) {
                //    $this->setErrors('Could not open file: '.$this->input);
                //    return false;
                //}
            } else {
                while ($data = fread($this->input, 1024)) {
                		$data=str_replace("é", "&acute;", $data);
                    if (!xml_parse($this->parser, str_replace("'", "&apos;", $data), feof($this->input))) {
                        $this->setErrors($this->getXmlError());
                        fclose($this->input);
                        return false;
                    }
                }
                fclose($this->input);
            }
            return true;
        }
     
        /****************************************************************************
            * @returns void
        ****************************************************************************/
        function free()
        {
            xml_parser_free($this->parser);
     
            if (!method_exists($this, '__destruct')) {
                unset($this);
            }
            else {
                $this->__destruct();
            }
        }
     
        /****************************************************************************
            * @private
            * @returns string
        ****************************************************************************/
        function getXmlError()
        {
            return sprintf("XmlParse error: %s at line %d", xml_error_string(xml_get_error_code($this->parser)), xml_get_current_line_number($this->parser));
        }
     
        /*---------------------------------------------------------------------------
            Custom Handler Methods
        ---------------------------------------------------------------------------*/
     
        /****************************************************************************
            Adds a callback function to be called when a tag is encountered.<br>
            Functions that are added must be of the form:<br>
            <b>functionName( $attributes )</b>
            * @param $tagName string.  The name of the tag currently being parsed.
            * @param $functionName string.  The name of the function in XmlDocument's
            subclass.
            * @returns void
        ****************************************************************************/
        function addTagHandler(&$tagHandler)
        {
            $name = $tagHandler->getName();
            if (is_array($name)) {
                foreach ($name as $n) {
                    $this->tagHandlers[$n] =& $tagHandler;
                }
            } else {
                $this->tagHandlers[$name] =& $tagHandler;
            }
        }
     
     
        /*---------------------------------------------------------------------------
            Private Handler Methods
        ---------------------------------------------------------------------------*/
     
        /****************************************************************************
            Callback function that executes whenever a the start of a tag
            occurs when being parsed.
            * @param $parser int.  The handle to the parser.
            * @param $tagName string.  The name of the tag currently being parsed.
            * @param $attributesArray attay.  The list of attributes associated with
            the tag.
            * @private
            * @returns void
        ****************************************************************************/
        function handleBeginElement($parser, $tagName, $attributesArray)
        {
            array_push($this->tags, $tagName);
            $this->level++;
            if (isset($this->tagHandlers[$tagName]) && is_subclass_of($this->tagHandlers[$tagName], 'xmltaghandler')) {
                $this->tagHandlers[$tagName]->handleBeginElement($this, $attributesArray);
            } else {
    			$this->handleBeginElementDefault($parser, $tagName, $attributesArray);
    		}
        }
     
        /****************************************************************************
            Callback function that executes whenever the end of a tag
            occurs when being parsed.
            * @param $parser int.  The handle to the parser.
            * @param $tagName string.  The name of the tag currently being parsed.
            * @private
            * @returns void
        ****************************************************************************/
        function handleEndElement($parser, $tagName)
        {
            array_pop($this->tags);
            if (isset($this->tagHandlers[$tagName]) && is_subclass_of($this->tagHandlers[$tagName], 'xmltaghandler')) {
                $this->tagHandlers[$tagName]->handleEndElement($this);
            } else {
    			$this->handleEndElementDefault($parser, $tagName);
    		}
            $this->level--;
        }
     
        /****************************************************************************
            Callback function that executes whenever character data is encountered
            while being parsed.
            * @param $parser int.  The handle to the parser.
            * @param $data string.  Character data inside the tag
            * @returns void
        ****************************************************************************/
        function handleCharacterData($parser, $data)
        {
            $tagHandler =& $this->tagHandlers[$this->getCurrentTag()];
            if (isset($tagHandler) && is_subclass_of($tagHandler, 'xmltaghandler')) {
                $tagHandler->handleCharacterData($this, $data);
            } else {
    			$this->handleCharacterDataDefault($parser, $data);
    		}
        }
     
        /****************************************************************************
            * @param $parser int.  The handle to the parser.
            * @returns void
        ****************************************************************************/
        function handleProcessingInstruction($parser, &$target, &$data)
        {
    //        if($target == 'php') {
    //            eval($data);
    //        }
        }
     
        /****************************************************************************
            * @param $parser int.  The handle to the parser.
            * @returns void
        ****************************************************************************/
        function handleDefault($parser, $data)
        {
     
        }
     
        /****************************************************************************
            * @param $parser int.  The handle to the parser.
            * @returns void
        ****************************************************************************/
        function handleUnparsedEntityDecl($parser, $entityName, $base, $systemId, $publicId, $notationName)
        {
     
        }
     
        /****************************************************************************
            * @param $parser int.  The handle to the parser.
            * @returns void
        ****************************************************************************/
        function handleNotationDecl($parser, $notationName, $base, $systemId, $publicId)
        {
     
        }
     
        /****************************************************************************
            * @param $parser int.  The handle to the parser.
            * @returns void
        ****************************************************************************/
        function handleExternalEntityRef($parser, $openEntityNames, $base, $systemId, $publicId)
        {
     
        }
     
    	/**
    	 * The default tag handler method for a tag with no handler
    	 *
    	 * @abstract
    	 */
    	function handleBeginElementDefault($parser, $tagName, $attributesArray)
    	{
    	}
     
    	/**
    	 * The default tag handler method for a tag with no handler
    	 *
    	 * @abstract
    	 */
    	function handleEndElementDefault($parser, $tagName)
    	{
    	}
     
    	/**
    	 * The default tag handler method for a tag with no handler
    	 *
    	 * @abstract
    	 */
    	function handleCharacterDataDefault($parser, $data)
    	{
    	}
     
    	/**
    	 * Sets error messages
    	 *
    	 * @param	$error	string	an error message
    	 */
        function setErrors($error)
        {
            $this->errors[] = trim($error);
        }
     
    	/**
    	 * Gets all the error messages
    	 *
    	 * @param	$ashtml	bool	return as html?
    	 * @return	mixed
    	 */
        function &getErrors($ashtml = true)
        {
            if (!$ashtml) {
                return $this->errors;
            } else {
            	$ret = '';
            	if (count($this->errors) > 0) {
                	foreach ($this->errors as $error) {
                	    $ret .= $error.'<br />';
                	}
            	}
            	return $ret;
            }
        }
    }
     
    ?>
    [/code]

  6. #6
    Membre expert Avatar de KiLVaiDeN
    Profil pro
    Inscrit en
    Octobre 2003
    Messages
    2 860
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2003
    Messages : 2 860
    Points : 3 444
    Points
    3 444
    Par défaut
    Il ne faut pas qu'il y ait de caractères accentués dans ton XML, mais des htmlentities comme j'ai dit au début : donc au lieu d'un é par exemple tu auras un &eacute;

    Ca ressemblerait à ça :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    ...
    <item  class="xv_titre" libelle="Monsieur" libelle_court="M." formule="Je vous prie de croire, Monsieur, en mes salutations distingu&eacute;es"/>
    ...

Discussions similaires

  1. Réponses: 12
    Dernier message: 20/08/2006, 22h35
  2. re-compilation de php non effective
    Par julien.63 dans le forum Apache
    Réponses: 3
    Dernier message: 25/07/2006, 08h10
  3. index.php non interprété directement
    Par Celeborn dans le forum Apache
    Réponses: 3
    Dernier message: 04/07/2006, 14h21
  4. php non validé
    Par lefelinherbivore dans le forum Langage
    Réponses: 1
    Dernier message: 30/06/2006, 05h59
  5. variables php non recharges dans flash
    Par TekiNico dans le forum Flash
    Réponses: 2
    Dernier message: 10/01/2006, 23h40

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