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

Bibliothèques et frameworks PHP Discussion :

[XML] problème de formatage d'un XML


Sujet :

Bibliothèques et frameworks PHP

  1. #1
    Expert confirmé
    Avatar de GLDavid
    Homme Profil pro
    Service Delivery Manager
    Inscrit en
    Janvier 2003
    Messages
    2 859
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 47
    Localisation : France, Seine Saint Denis (Île de France)

    Informations professionnelles :
    Activité : Service Delivery Manager
    Secteur : Industrie Pharmaceutique

    Informations forums :
    Inscription : Janvier 2003
    Messages : 2 859
    Points : 4 812
    Points
    4 812
    Par défaut [XML] problème de formatage d'un XML
    Bonjour

    Une petite colle avant le week-end. J'ai fichier PHP suivant :
    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
     
    <?php
    /*
     * Created on 09-févr.-2006
     *
     * To change the template for this generated file go to
     * Window - Preferences - PHPeclipse - PHP - Code Templates
     */
     require("processXML.php");
     
     function write_array($array){
     	foreach($array as $r){
     		if(is_array($r)){
     			echo"</br>";
     			write_array($r);
     		}
     		else {
     			echo"$r ";
     		}
     	}
     }
     
     //$array = array("<html><body>","Bonjour tout le monde","</body>", "<html>");
     $array = array(array("France"=>array("Marseille")),
     				array("Canada"=>array("Quebec", "Montreal")),
     				array("HTML"=>array("<html><body>", "Hello world !", "</body>", "</html>")),
     				array("Complex"=>array("truc", "Dur"=>array("Toto", "Tata", "Tutu"), "muche")),
     				array("Belgique"=>array("Namur", "Bruxelles"))
     				);
     $xmlfile = "toto.xml";
     $array2 = NULL;
     
     echo "Original array:</br>";
     write_array($array);
     echo"</br>";
     
     $r = array_to_xml($array, 1);
     if(!isset($r)){
     	die ("Plantage 1 !!!");
     }
     else {
     	$id_file = fopen($xmlfile, "w");
    	if (fwrite($id_file, $r) == FALSE) {
    		die ("Plantage 2 !!!</br>");
    	}
    	fclose($id_file);
     	echo "A look to the produced <a href=\"./$xmlfile\" target=\"BLANK\">XML</a></br>";
     }
    Et le fichier appelé est le suivant :
    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
     
    <?php
     
    /*
     * Created on 09-févr.-2006
     *
     * To change the template for this generated file go to
     * Window - Preferences - PHPeclipse - PHP - Code Templates
     */
     
    $array=NULL;
     
    function array_to_xml($array, $level = 1) {
    	$variable="scalar";
    	$tab = "array";
    	$xml = '';
    	if ($level == 1) {
    		$xml .= '<?xml version="1.0"?>'."\n<root>\n";
    	}
    	foreach ($array as $key => $value) {
    		if (is_array($value)) {
    			foreach ($value as $key2 => $value2) {
    				if (is_array($value2)) {
    					$xml .= str_repeat("\t", $level)."<$tab key=\"$key2\">\n";
    					$xml .= array_to_xml($value2, $level+1);
    					$xml .= str_repeat("\t", $level)."</$tab>\n";
    				} else {
    					$value2 = htmlentities($value2, ENT_QUOTES, "ISO-8859-1");
    					if (trim($value2) != '') {
    						if (htmlspecialchars($value2) != $value2) {
    							$xml .= str_repeat("\t", $level)."<$variable key=\"$key2\"><![CDATA[$value2]]>"."</$variable>\n";
    						} else {
    							$xml .= str_repeat("\t", $level)."<$variable key=\"$key2\">$value2</$variable>\n";
    						}
    					}
    				}
    			}
    		} else {
    			$value = htmlentities($value, ENT_QUOTES, "ISO-8859-1");
    			if (trim($value) != '') {
    				if (htmlspecialchars($value) != $value) {
    					$xml .= str_repeat("\t", $level)."<$variable key=\"$key\">"."<![CDATA[$value]]></$variable>\n";
    				} else {
    					$xml .= str_repeat("\t", $level)."<$variable key=\"$key\">$value</$variable>\n";
    				}
    			}
    		}
    	}
    	if ($level == 1) {
    		$xml .= "</root>\n";
    	}
    	return $xml;
    }
    ?>
    Je veux donc retranscrire le tableau du premier fichier en 1 fichier XML. L'ennui est que si l'une des données contenu dans un tableau lui-même contenu dans un tableau, cela ne se traduit pas par un pallier supplémentaire dans mon XML. Ai-je raté une étape ?
    Merci d'avance de vos réponses.

    @++

  2. #2
    Invité
    Invité(e)
    Par défaut
    tu veux exporter un tableau en xml ?

    si oui, regarde ce que j'ai trouvé : http://gvtulder.f2o.org/notities/arraytoxml/
    Pas besoin de réinventer la roue.

  3. #3
    Expert confirmé
    Avatar de siddh
    Inscrit en
    Novembre 2005
    Messages
    3 868
    Détails du profil
    Informations personnelles :
    Âge : 48

    Informations forums :
    Inscription : Novembre 2005
    Messages : 3 868
    Points : 5 011
    Points
    5 011
    Par défaut
    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
     
    function array_to_xml($array, $level = 1) {
       $variable="scalar";
       $tab = "array";
       $xml = '';
       if ($level == 1) {
          $xml .= '<?xml version="1.0"?>'."\n<root>\n";
       }
       foreach ($array as $key => $value) {
     
          if (is_array($value)) {
    		  if(!is_numeric($key)){
    			  $xml .= str_repeat("\t", $level)."<$tab key=\"$key\">\n";
    			  $xml .= array_to_xml($value, $level+1);
    			  $xml .= str_repeat("\t", $level)."</$tab>\n";
              }
    		  else
    			$xml .= array_to_xml($value, $level+1);
          } else {
             $value = htmlentities($value, ENT_QUOTES, "ISO-8859-1");
             if (trim($value) != '') {
                if (htmlspecialchars($value) != $value) {
                   $xml .= str_repeat("\t", $level)."<$variable key=\"$key\">"."<![CDATA[$value]]></$variable>\n";
                } else {
                   $xml .= str_repeat("\t", $level)."<$variable key=\"$key\">$value</$variable>\n";
                }
             }
          }
       }
       if ($level == 1) {
          $xml .= "</root>\n";
       }
       return $xml;
    }
    je sais pas si c'est ça que tu veux comme résultat

  4. #4
    Expert confirmé
    Avatar de GLDavid
    Homme Profil pro
    Service Delivery Manager
    Inscrit en
    Janvier 2003
    Messages
    2 859
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 47
    Localisation : France, Seine Saint Denis (Île de France)

    Informations professionnelles :
    Activité : Service Delivery Manager
    Secteur : Industrie Pharmaceutique

    Informations forums :
    Inscription : Janvier 2003
    Messages : 2 859
    Points : 4 812
    Points
    4 812
    Par défaut
    Ca a l'air d'être très good siddh !
    Merci à toi pour cette aide, il me manquait l'évaluation d'un if.

    @++

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Problème de formatage
    Par philippef dans le forum Langage
    Réponses: 2
    Dernier message: 20/11/2005, 17h32
  2. [HTML] Tableau problème de formatage avec internet Explorer
    Par rabobsky dans le forum Balisage (X)HTML et validation W3C
    Réponses: 2
    Dernier message: 18/11/2005, 09h50
  3. Réponses: 3
    Dernier message: 13/10/2005, 21h39
  4. Problème de formatage de disque dur
    Par Marekh dans le forum Composants
    Réponses: 10
    Dernier message: 04/07/2005, 14h08
  5. problème de formatage à l'écran
    Par bidson dans le forum XMLRAD
    Réponses: 13
    Dernier message: 11/07/2003, 18h02

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