Bonjour, voici mon script php :

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?
$xml = new DomDocument();
$xml->load('bourse.xml');
$elements = $xml->getElementsByTagName('bourse');
$element = $elements->item(0);
$child = $element->childNodes;
 
foreach($child as $enfant) {
		$nom = $enfant->nodeName;
		if ($nom == "acier") { $ac = $enfant->nodeValue; }
		elseif ($nom == "hydro") { $hy = $enfant->nodeValue; }
		elseif ($nom == "oxy") { $ox = $enfant->nodeValue; }
		elseif ($nom == "soufre") { $so = $enfant->nodeValue; }
		elseif ($nom == "xenon") { $xe = $enfant->nodeValue; }
		elseif ($nom == "titane") { $ti = $enfant->nodeValue; }
		elseif ($nom == "lithium") { $li = $enfant->nodeValue; }
		elseif ($nom == "zinc") { $zi = $enfant->nodeValue; }
		elseif ($nom == "mercure") { $me = $enfant->nodeValue; }
		elseif ($nom == "uranium") { $ur = $enfant->nodeValue; }
		elseif ($nom == "energie") { $en = $enfant->nodeValue; }
}
 
class Ressource {
	public $value;
	public $less;
	public $most;
	public $incrl;
	public $incrm;
	public $div;
 
	function __construct() {
		$this->div = 1000;
	}
 
	function make($val, $les, $mos, $inl, $inm) {
	  $this->value = $val;
      $this->less = $les;
      $this->most = $mos;
      $this->incrl = $inl;
      $this->incrm = $inm;
	}
 
	function update() {
      $incr = rand($this->incrl, $this->incrm);
      $this->value += $incr / $this->div;
      if ($this->value <= $this->less) { $this->value += 2 * $this->incrm / 1000; }
      elseif ($this->value >= $this->most) { $this->value -= 2 * $this->incrm / 1000; }
	}
}
 
$acier = new Ressource();
$acier->make($ac, 0.4, 123, -480, 500);
$acier->update();
$hydro = new Ressource();
$hydro->make($hy, 0.5, 156, -470, 490);
$hydro->update();
$oxy = new Ressource();
$oxy->make($ox, 0.5, 156, -470, 490);
$oxy->update();
$soufre = new Ressource();
$soufre->make($so, 0.4, 217, -430, 450);
$soufre->update();
$xenon = new Ressource();
$xenon->make($xe, 0.8, 1209, -280, 300);
$xenon->update();
$titane = new Ressource();
$titane->make($ti, 0.6, 213, -440, 460);
$titane->update();
$lithium = new Ressource();
$lithium->make($li, 0.3, 513, -730, 750);
$lithium->update();
$zinc = new Ressource();
$zinc->make($zi, 0.6, 190, -440, 460);
$zinc->update();
$mercure = new Ressource();
$mercure->make($me, 0.7, 784, -530, 550);
$mercure->update();
$uranium = new Ressource();
$uranium->make($ur, 0.9, 2165, -820, 840);
$uranium->update();
$energie = new Ressource();
$energie->make($en, 0.5, 490, -540, 560);
$energie->update();
 
$file = fopen("bourse.xml", "r+");
$line = fputs($file, '<?xml version="1.0"?>
<bourse>
   <acier>'.$acier->value.'</acier>
   <hydro>'.$hydro->value.'</hydro>
   <oxy>'.$oxy->value.'</oxy>
   <soufre>'.$soufre->value.'</soufre>
   <xenon>'.$xenon->value.'</xenon>
   <titane>'.$titane->value.'</titane>
   <lithium>'.$lithium->value.'</lithium>
   <zinc>'.$zinc->value.'</zinc>
   <mercure>'.$mercure->value.'</mercure>
   <uranium>'.$uranium->value.'</uranium>
   <energie>'.$energie->value.'</energie>
</bourse>');
fclose($file);
?>
Et mon fichier XML :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
<bourse>
   <acier>1.163</acier>
   <hydro>0.884</hydro>
   <oxy>1.348</oxy>
   <soufre>0.933</soufre>
   <xenon>0.864</xenon>
   <titane>0.995</titane>
   <lithium>1.417</lithium>
   <zinc>0.657</zinc>
   <mercure>1.65</mercure>
   <uranium>1.358</uranium>
   <energie>1.505</energie>
</bourse>
...par exemple.

Le problème est que, une fois sur deux, ça me met :

Warning: DOMDocument::load() [function.DOMDocument-load]: Extra content at the end of the document in /mnt/140/sdb/1/8/0tt0k4r/bourse.xml, line: 14 in /mnt/140/sdb/1/8/0tt0k4r/bourse.php on line 57

Warning: Invalid argument supplied for foreach() in /mnt/140/sdb/1/8/0tt0k4r/bourse.php on line 62


Avez vous une quelconque solution ??


Merci infiniment,