Bonjour,

Je teste le DOMDocument de PHP via l'exemple suivant que je trouve en commentaire dans la documentation: https://www.php.net/manual/fr/class.domdocument.php

xml_album a comme fils xml_track et xml_second. Puis, xml_track à son tour comme fils xml_note. Je n'arrive pas à faire en sorte que xml_second a comme fils xml_second_note.


Voilà le code:

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
 
// Set the content type to be XML
header("Content-Type: application/xml; charset=ISO-8859-15");
 
// "Create" the document.
$xml = new DOMDocument("1.0", "ISO-8859-15");
$xml->formatOutput = true; // pour un XML joliment indenté
 
// Create some elements.
$xml_album = $xml->createElement("Album");
$xml_track = $xml->createElement("Track", "The ninth symphony");
 
$xml_second = $xml->createElement("Second", "Album de Michael Jackson");
 
// Set the attributes.
$xml_track->setAttribute("length", "0:01:15");
$xml_track->setAttribute("bitrate", "64kb/s");
$xml_track->setAttribute("channels", "2");
 
$xml_second->setAttribute("length", "0:02:30");
$xml_second->setAttribute("bitrate", "120kb/s");
$xml_second->setAttribute("channels", "3");
 
// Create another element.
$xml_note = $xml->createElement("Note", "The last symphony composed by Ludwig van Beethoven.");
 
//$xml_second_note = $xml->createElement("Second note", "A very important note");
 
// Append the elements.
$xml_track->appendChild($xml_note);
$xml_album->appendChild($xml_track);
 
//$xml_second->appendChild($xml_second_note);
$xml_album->appendChild($xml_second);
 
// Ajoute la racine au document
$xml->appendChild($xml_album);
 
// Affiche le XML
echo $xml->saveXML();
Quand ces deux lignes là sont en commentaire,

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
 
//$xml_second_note = $xml->createElement("Second note", "A very important note");
 
//$xml_second->appendChild($xml_second_note);
cela fonctionne et j'ai à l'écran:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
 
<Album>
<Track length="0:01:15" bitrate="64kb/s" channels="2">
The ninth symphony
<Note>
The last symphony composed by Ludwig van Beethoven.
</Note>
</Track>
<Second length="0:02:30" bitrate="120kb/s" channels="3">Album de Michael Jackson</Second>
</Album>
Par contre, quand les deux lignes sont décommentés, je n'arrive pas à avoir $xml_second_note comme fils de $xml_second et j'ai une erreur 500.

Savez vous ce qui fait que cela ne fonctionne pas correctement s'il vous plaît?

Je vous remercie par avance,