Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
3406 connectés 

 


Dernière réponse
Sujet : [PHP] utiliser le parser XML
z0rglub personnellement je me suis fait mon propre "parser" hyper basique, qui t'impose des truc au niveau de ton XML mais qui a le mérite de marcher sur tous les serveurs. 3 fonctions :

Code :
  1. function getContent( $element, $node )
  2. {
  3.  $content = str_replace( "<".$node.">", "", $element );
  4.  $content = str_replace( "</".$node.">", "", $content );
  5.  return $content;
  6. }
  7. function getChild( $document, $node )
  8. {
  9.  preg_match("/\<".$node.">.*\<\/".$node."\>/U", $document, $retour);
  10.  return $retour[0];
  11. }
  12. function getChildren( $document, $node )
  13. {
  14.  preg_match_all("/\<".$node.">.*\<\/".$node."\>/U", $document, $retour);
  15.  return $retour[0];
  16. }
  17. function get_CodeXML( $filename )
  18. {
  19.  $file = fopen( $filename, "r" );
  20.  if ( !$file )
  21.  {
  22.   echo "<p>Impossible d'ouvrir le fichier</p>";
  23.   exit();
  24.  }
  25.  while ( !feof( $file ) )
  26.  {
  27.   $line .= fgets( $file, 1024 );
  28.  }
  29.  fclose( $file );
  30.  $line = str_replace("\n","",$line);
  31.  $line = str_replace("\t","",$line);
  32.  return $line;
  33. }


un exemple de fichier xml :

Code :
  1. <?xml version="1.0" encoding="ISO-8859-1"?>
  2. <cv>
  3. <conf>
  4.  <font>Arial, Verdana, Sans-Serif</font>
  5.                 ...
  6. </conf>
  7.         ...
  8.         <line>
  9.   <titre>Système d'exploitation</titre>
  10.   <data>Windows 98, NT et 2000. Linux.</data>
  11.  </line>
  12.  <line>
  13.   <titre>Programmation</titre>
  14.   <data>PHP, HTML (XHTML), XML, Java, C++ (dont Visual C++), Corba, Delphi.</data>
  15.  </line>....


 
et comment je l'utilise :

Code :
  1. $document = get_CodeXML( $fichierXml );
  2. $conf = getContent( getChild( $document, "conf" ), "conf" );
  3. $font = getContent( getChild( $conf, "font" ), "font" );
  4. ....
  5. $list_infos = getChildren( $list_bloc[$i], "line" );
  6. for ( $j = 0; $j < sizeof( $list_infos ); $j++ )
  7. {...}


 
si tu veux plus d'infos, je t'en donnerais.
regarde ce que ça donnes : www.z0rglub.com et www.z0rglub.com/pierrick.xml


Votre réponse
Nom d'utilisateur    Pour poster, vous devez être inscrit sur ce forum .... si ce n'est pas le cas, cliquez ici !
Le ton de votre message                        
                       
Votre réponse


[b][i][u][strike][spoiler][fixed][cpp][url][email][img][*]   
 
   [quote]
 

Options

 
Vous avez perdu votre mot de passe ?


Vue Rapide de la discussion
z0rglub personnellement je me suis fait mon propre "parser" hyper basique, qui t'impose des truc au niveau de ton XML mais qui a le mérite de marcher sur tous les serveurs. 3 fonctions :

Code :
  1. function getContent( $element, $node )
  2. {
  3.  $content = str_replace( "<".$node.">", "", $element );
  4.  $content = str_replace( "</".$node.">", "", $content );
  5.  return $content;
  6. }
  7. function getChild( $document, $node )
  8. {
  9.  preg_match("/\<".$node.">.*\<\/".$node."\>/U", $document, $retour);
  10.  return $retour[0];
  11. }
  12. function getChildren( $document, $node )
  13. {
  14.  preg_match_all("/\<".$node.">.*\<\/".$node."\>/U", $document, $retour);
  15.  return $retour[0];
  16. }
  17. function get_CodeXML( $filename )
  18. {
  19.  $file = fopen( $filename, "r" );
  20.  if ( !$file )
  21.  {
  22.   echo "<p>Impossible d'ouvrir le fichier</p>";
  23.   exit();
  24.  }
  25.  while ( !feof( $file ) )
  26.  {
  27.   $line .= fgets( $file, 1024 );
  28.  }
  29.  fclose( $file );
  30.  $line = str_replace("\n","",$line);
  31.  $line = str_replace("\t","",$line);
  32.  return $line;
  33. }


un exemple de fichier xml :

Code :
  1. <?xml version="1.0" encoding="ISO-8859-1"?>
  2. <cv>
  3. <conf>
  4.  <font>Arial, Verdana, Sans-Serif</font>
  5.                 ...
  6. </conf>
  7.         ...
  8.         <line>
  9.   <titre>Système d'exploitation</titre>
  10.   <data>Windows 98, NT et 2000. Linux.</data>
  11.  </line>
  12.  <line>
  13.   <titre>Programmation</titre>
  14.   <data>PHP, HTML (XHTML), XML, Java, C++ (dont Visual C++), Corba, Delphi.</data>
  15.  </line>....


 
et comment je l'utilise :

Code :
  1. $document = get_CodeXML( $fichierXml );
  2. $conf = getContent( getChild( $document, "conf" ), "conf" );
  3. $font = getContent( getChild( $conf, "font" ), "font" );
  4. ....
  5. $list_infos = getChildren( $list_bloc[$i], "line" );
  6. for ( $j = 0; $j < sizeof( $list_infos ); $j++ )
  7. {...}


 
si tu veux plus d'infos, je t'en donnerais.
regarde ce que ça donnes : www.z0rglub.com et www.z0rglub.com/pierrick.xml

kadreg

zion a écrit a écrit :

 




 
[:tapai]

zion :ange:
kadreg :heink:  
 
C'est quoi ce TRES vieux bide ?
zion oui oui, c'est dans phpinfo  :)
kadreg 'lu
 
Cherchant à lire un fichier XML afin de générer une page avec php (3), j'ai été fouillé dans la librairie XML de php (celle qui utilise expat). Mais j'ai un message d'erreur qui me dit que xml_parser_create n'existe pas. Comment vérifier si XML est installé dans PHP (c'est ecrit par phpinfo () ? )

 


Copyright © 1997-2025 Groupe LDLC (Signaler un contenu illicite / Données personnelles)