Le code initial me donne :
<?xml version="1.0" encoding="ISO-8859-15"?>
<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>
Mais ça n'a ni queue ni tête, je ne pense pas que c'est ce que tu veux.
Pour obtenir ceci :
<?xml version="1.0" encoding="UTF-8"?>
<albums>
<album title="Thriller" artist="Michael Jackson">
<note>Note sur l'album de M. Jackson</note>
<tracks>
<track position="1" title="Wanna Be Startin' Somethin'" length="06:02"/>
<track position="2" title="Baby Be Mine" length="04:20"/>
</tracks>
</album>
<album title="9e symphonie" artist="Ludwig Van Beethoven"/>
</albums>
Tu peux faire cela :
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
| <?php
// header('Content-Type: application/xml; charset=UTF-8');
header('Content-Type: text/plain; charset=UTF-8'); // Pour visu brut lors du développement
$dom = new DOMDocument('1.0', 'UTF-8');
$dom->formatOutput = true;
// Création de la racine
$albums = $dom->createElement('albums');
$dom->appendChild($albums);
// Création d'un album
$album = $dom->createElement('album');
$albums->appendChild($album);
$album->setAttribute('title', 'Thriller');
$album->setAttribute('artist', 'Michael Jackson');
// Création d'une note
$note = $dom->createElement('note', 'Note sur l\'album de M. Jackson');
$album->appendChild($note);
// Création des pistes
$tracks = $dom->createElement('tracks');
$album->appendChild($tracks);
// Création d'une piste
$track = $dom->createElement('track');
$tracks->appendChild($track);
$track->setAttribute('position', 1);
$track->setAttribute('title', 'Wanna Be Startin\' Somethin\'');
$track->setAttribute('length', '06:02');
// Création d'une autre piste
$track = $dom->createElement('track');
$tracks->appendChild($track);
$track->setAttribute('position', 2);
$track->setAttribute('title', 'Baby Be Mine');
$track->setAttribute('length', '04:20');
// Répéter pour un nouvel album
// ...
$album = $dom->createElement('album');
$albums->appendChild($album);
$album->setAttribute('title', '9e symphonie');
$album->setAttribute('artist', 'Ludwig Van Beethoven');
// ...
echo $dom->saveXML();
exit(0); |
Si ça ne fonctionne pas donne le message d'erreur précis tel qu'indiqué par mathieu.
Partager