weed | Voilou je dois parser un fichier xml et j'ai un petit souci avec fread. Comme 2ème argument de fread, il faut indiquer la taille du bloc à lire. J'avais mis 1024 au début mais je me suis vite apercus que certaines balises de mon fichier XML n'etait pas inserer dans ma base de donnée. J'ai alors augmenté la longueur à 2024 et bingo, c'etait ca. Le problème c'est que j'aimerais lire ligne par ligne mais si je mets un nombre trop petit, ca va lire la moitié de la ligne, si je mets trop grand, il va y avoir un décalage. Ca va lire la ligne en entiere et une partie de ligne suivante, ce qui fait que ca fait merder mon parsage. La solution serait de charger tout dans une variable mais c'est pas tres propre comme solution : fread ($fd, filesize ($filename))
Je sais pas trop quoi faire, comment faire ? J'ai regardé, dans les tutos dont celui de commentcamarche, zend et ils font pareil que moi. http://www.commentcamarche.net/php/phpxml.php3
http://www.zend.com/zend/art/parsing.php
Il parse au fur et mesure de la lecture du fichier mais leur systeme n'est pas du tout fiable non plus.
voici mon bout de code si cela peut servir ...
PS : je suis en PHP4 bien sur sinon je me serais renseigné sur le fameux simpleXML
Code :
- function exportToMySQL ($tablename, $champs)
- {
- $this->parser = xml_parser_create();
- xml_set_object($this->parser, $this);
- xml_set_element_handler ($this->parser, 'OpenTag', 'CloseTagToMySQL');
- xml_set_character_data_handler($this->parser, 'donnee');
- while ($fd = parsingXML::LectureFichier ())
- {
- echo "<br><br>lecture de fichier = ".$fd;
- parsingXML::ParseData ($fd);
- }
- }
- //méthode privée
- function ParseDaTa ($data)
- {
- if(!xml_parse($this->parser, $data))
- {
- die(sprintf("<br>XML error: %s at line %d",
- xml_error_string(xml_get_error_code($this->parser)), xml_get_current_line_number($this->parser)));
-
- }
- }
- function LectureFichier ()
- {
- static $fd;
- if ($fd == "" )
- $fd = fopen ($this->fichier, "r" );
- if (feof($fd))
- { fclose ($fd);
- return FALSE;
- }
- else
- {
- return fread ($fd, 2024);
- }
- }
- function OpenTag ($parser, $tagName, $attribu)
- {
- echo "<br>OpenTag : ".$tagName;
- $attribu = implode("|", $attribu);
- $this->TabLine[$tagName]="";
- $this->TagNameCurrent = $tagName;
- if (trim ($attribu) != "" )
- $this->TabLine[$tagName]=$attribu;
- }
- function CloseTagToMySQL ($parser, $tagName)
- {
- if (trim($tagName) == $this->NewLine)
- {
- $q = "INSERT `INDEX_EUROPE` set TRANSACTION_ID='".$this->TabLine['TRANSACTION']."', MSISDN='".$this->TabLine['MSISDN']."',
- DATE='".$this->TabLine['DATE']."', PRICE='".$this->TabLine['PRICE']."', CURRENCY='".$this->TabLine['CURRENCY']."', TAG='".$this->TabLine['TAG']."',
- MODE='".$this->TabLine['MODE']."', STATUS='".$this->TabLine['STATUS']."', NOTICE='".$this->TabLine['NOTICE']."', PRODUCT_ID='".$this->TabLine['PRODUCT']."',
- TYPE='".$this->TabLine['TYPE']."', LABEL='".$this->TabLine['LABEL']."'
- ";
- $rm = mysql_query ($q);
- if (!$rm)
- echo ('<br><br>Requête invalide : ' . mysql_error());
- }
- }
- function donnee ($parser, $data)
- {
- $data = addslashes ($data);
- if (trim ($data) != "" )
- {
- $this->TabLine[$this->TagNameCurrent] = $data;
- }
- $this->TagNameCurrent = "";
- }
- }
|
|