[XML] problème de formatage d'un XML
Bonjour
Une petite colle avant le week-end. J'ai fichier PHP suivant :
Code:
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:
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.
@++