Modérateur PHPfrance |
8758 Messages
21 août 2011, 15:06
hum au pif je dirais que c'est parce que c'est pas du xml complement valide ? (pas de début fin etc)
aller j'te mâche un peu le travail

(c'est juste tiré de la doc que je t'ai déjà filé)
<?php
$string = <<<xml
<root>
<item>
<quad style="Bgs1InRace" substyle="BgList" posn="-61.500 -8.000 4.000" sizen="60.000 63.000" align="center" valign="center"/>
<label text="\$i\$fc0id : 496" posn="-2 20.5 5" autonewline="1" scale="1" halign="right" valign="center"/>
</item>
<item>
<quad style="Bgs1InRace" substyle="BgList" posn="-61.500 -8.000 4.000" sizen="60.000 63.000" align="center" valign="center"/>
<label text="\$i\$fc0id : 496" posn="-2 20.5 5" autonewline="1" scale="1" halign="right" valign="center"/>
</item>
</root>
xml;
/*
* Xfinal = Xdépart *320/128
Yfinal = Ydépart *180/96
*/
$xml = simplexml_load_string($string);
echo '<pre>';
var_dump($xml);
echo '</pre>';
echo '<hr />';
foreach($xml->item as $it ) {
$quadAtt = $it->quad->attributes();
$l = explode(' ',$quadAtt->sizen);
if (isset($l[0])) $l[0] = $l[0]*320/128;
if (isset($l[1])) $l[1] = $l[1]*180/96;
$quadAtt-> sizen = implode(' ',$l);
$quadAtt = $it->quad->attributes();
$l = explode(' ',$quadAtt->posn);
if (isset($l[0])) $l[0] = $l[0]*320/128;
if (isset($l[1])) $l[1] = $l[1]*180/96;
$quadAtt-> posn = implode(' ',$l);
}
echo '<pre>';
var_dump($xml);
echo '</pre>';
?>
résultat :
Code : Tout sélectionner
object(SimpleXMLElement)#1 (1) {
["item"]=>
array(2) {
[0]=>
object(SimpleXMLElement)#2 (2) {
["quad"]=>
object(SimpleXMLElement)#4 (1) {
["@attributes"]=>
array(6) {
["style"]=>
string(10) "Bgs1InRace"
["substyle"]=>
string(6) "BgList"
["posn"]=>
string(20) "-61.500 -8.000 4.000"
["sizen"]=>
string(13) "60.000 63.000"
["align"]=>
string(6) "center"
["valign"]=>
string(6) "center"
}
}
["label"]=>
object(SimpleXMLElement)#5 (1) {
["@attributes"]=>
array(6) {
["text"]=>
string(14) "$i$fc0id : 496"
["posn"]=>
string(9) "-2 20.5 5"
["autonewline"]=>
string(1) "1"
["scale"]=>
string(1) "1"
["halign"]=>
string(5) "right"
["valign"]=>
string(6) "center"
}
}
}
[1]=>
object(SimpleXMLElement)#3 (2) {
["quad"]=>
object(SimpleXMLElement)#5 (1) {
["@attributes"]=>
array(6) {
["style"]=>
string(10) "Bgs1InRace"
["substyle"]=>
string(6) "BgList"
["posn"]=>
string(20) "-61.500 -8.000 4.000"
["sizen"]=>
string(13) "60.000 63.000"
["align"]=>
string(6) "center"
["valign"]=>
string(6) "center"
}
}
["label"]=>
object(SimpleXMLElement)#4 (1) {
["@attributes"]=>
array(6) {
["text"]=>
string(14) "$i$fc0id : 496"
["posn"]=>
string(9) "-2 20.5 5"
["autonewline"]=>
string(1) "1"
["scale"]=>
string(1) "1"
["halign"]=>
string(5) "right"
["valign"]=>
string(6) "center"
}
}
}
}
}
object(SimpleXMLElement)#1 (1) {
["item"]=>
array(2) {
[0]=>
object(SimpleXMLElement)#2 (2) {
["quad"]=>
object(SimpleXMLElement)#9 (1) {
["@attributes"]=>
array(6) {
["style"]=>
string(10) "Bgs1InRace"
["substyle"]=>
string(6) "BgList"
["posn"]=>
string(17) "-153.75 -15 4.000"
["sizen"]=>
string(11) "150 118.125"
["align"]=>
string(6) "center"
["valign"]=>
string(6) "center"
}
}
["label"]=>
object(SimpleXMLElement)#7 (1) {
["@attributes"]=>
array(6) {
["text"]=>
string(14) "$i$fc0id : 496"
["posn"]=>
string(9) "-2 20.5 5"
["autonewline"]=>
string(1) "1"
["scale"]=>
string(1) "1"
["halign"]=>
string(5) "right"
["valign"]=>
string(6) "center"
}
}
}
[1]=>
object(SimpleXMLElement)#3 (2) {
["quad"]=>
object(SimpleXMLElement)#7 (1) {
["@attributes"]=>
array(6) {
["style"]=>
string(10) "Bgs1InRace"
["substyle"]=>
string(6) "BgList"
["posn"]=>
string(17) "-153.75 -15 4.000"
["sizen"]=>
string(11) "150 118.125"
["align"]=>
string(6) "center"
["valign"]=>
string(6) "center"
}
}
["label"]=>
object(SimpleXMLElement)#9 (1) {
["@attributes"]=>
array(6) {
["text"]=>
string(14) "$i$fc0id : 496"
["posn"]=>
string(9) "-2 20.5 5"
["autonewline"]=>
string(1) "1"
["scale"]=>
string(1) "1"
["halign"]=>
string(5) "right"
["valign"]=>
string(6) "center"
}
}
}
}
}
ce qu'il te reste a faire :
- factoriser la chose pour faire une belle fonction et pas avoir 4000 paquets dans le foreach

- faire ce que j'ai fait pour "quad" pour 'label'
- renvoyer l'objet sous forme de chaîne de caractère pour, et ben en faire ce que tu veux

- si tu tiens au nb de zéro a près la virgule formater les nombres (
number_format)
pourquoi <root></root> ? parce qu'il faut bien un début et une fin à ton arbre xml

pourquoi <item></item> ? parce que ça permet d'en mettre plusieurs a la fois et de pas faire 44 fois une chose que tu peux faire qu'une
évolution ? un beau formulaire qui mâche tout le boulot aux personnes qui rentre les chiffres (une case par attribut et tu génère le xml toi même, ça peux éviter toutes ces acrobaties

)
@+
Il en faut peu pour être heureux ......